• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Allegro X PCB Editor
  3. Accepting user input with wildcards (regex help)

Stats

  • Replies 3
  • Subscribers 159
  • Views 14627
  • Members are here 0
More Content

Accepting user input with wildcards (regex help)

EvanShultz
EvanShultz over 9 years ago

I want to accept user input that contains wildcards for ref des and net names. The basic solution isn't difficult and I get good results with:

; build regular expression pattern of filter text
; put start and end anchors on text and all asterisks match "any character zero or more times"
rexCompile(buildString(parseString(strcat("^" "input_string" "$") "*") ".*"))

(I know I can use the PCRE functions, but I started with the basic ones since they worked.)

Trouble came when I tried to find pathological cases and realized that regex special chars are common in net names (especially "+" like in "+5V"). Some regex special chars are ".\*+^$".

Starting with the first problem I encountered, I was able to modify the following code to handle a plus sign:

pat = pcreCompile("[\\+]")
rexCompile(buildString(parseString(strcat("^" pcreReplace(pat "input_string" "\\\\+" 0) "$") "*") ".*"))

This does work and I can then accept an asterisk as a wildcard on the input and match "+5V", "+3.3V", etc. in my net names. But this is just one of the special chars. As the special chars could be in any position of the input string (ref des or net name), I don't see any one regex pattern to handle it all and doing them individually looks miserable. There must be a better way which I don't see. Searching on the forums and reading sklangref again haven't lit a bulb over my head, but it may be because I'm just daft.

Moving through the string character-by-character and replacing special chars with escaped versions would be an option, but that's rather painful and sure to incur a delay that users would notice on a big design (I tried using axlDBCloak while comparing the input string to the regex and didn't see a speed up, possibly because I'm only doing a database read?). The "Find by Name or Property" form does the same thing but query results appear instantly. It even handles multiple wildcards properly, so there's a way to do it well but I haven't figured it out. Can anyone help?

  • Sign in to reply
  • Cancel
Parents
  • EvanShultz
    EvanShultz over 9 years ago
    I was able to make a workable solution, though as ugly as I feared in my post above, by iterating through the input char string. Here's what I have:

    searchString = ""
    for(i 0 (strlen(QSC_fnData->filter) - 1)
    char = substring(QSC_fnData->filter (i + 1) 1)

    if(nindex(".*+^$" char) then
    if((char == "*") then
    searchString = strcat(searchString ".*")
    else
    searchString = strcat(searchString "\\" char)
    )
    else
    searchString = strcat(searchString char)
    )
    )
    rePattern = pcreCompile(strcat("^" searchString "$"))

    Basically I'm treating "*" differently from other special chars by making it a "any char 0 or more times" in the regex while any other special char just gets escaped so that it's an exact match, and then any other char just gets passed through. (Note that I will keep experimenting as I'm not sure the special chars are properly inclusive).

    I still have a feeling that this could be better. The input can be sanitized this way, and it's more robust than the above, but it looks ugly. Are there better methods?

    (Also, responsiveness begins to degrade with designs having more than about 500 nets. Somehow there must be a faster way to do it as I mentioned above that the Find by Name or Property form is immediate even with >2000 nets. Any ideas on how to make iterating through a list while performing pcreExecute on each list member, or a totally different method, would also be appreciated.)
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • EvanShultz
    EvanShultz over 9 years ago
    I was able to make a workable solution, though as ugly as I feared in my post above, by iterating through the input char string. Here's what I have:

    searchString = ""
    for(i 0 (strlen(QSC_fnData->filter) - 1)
    char = substring(QSC_fnData->filter (i + 1) 1)

    if(nindex(".*+^$" char) then
    if((char == "*") then
    searchString = strcat(searchString ".*")
    else
    searchString = strcat(searchString "\\" char)
    )
    else
    searchString = strcat(searchString char)
    )
    )
    rePattern = pcreCompile(strcat("^" searchString "$"))

    Basically I'm treating "*" differently from other special chars by making it a "any char 0 or more times" in the regex while any other special char just gets escaped so that it's an exact match, and then any other char just gets passed through. (Note that I will keep experimenting as I'm not sure the special chars are properly inclusive).

    I still have a feeling that this could be better. The input can be sanitized this way, and it's more robust than the above, but it looks ugly. Are there better methods?

    (Also, responsiveness begins to degrade with designs having more than about 500 nets. Somehow there must be a faster way to do it as I mentioned above that the Find by Name or Property form is immediate even with >2000 nets. Any ideas on how to make iterating through a list while performing pcreExecute on each list member, or a totally different method, would also be appreciated.)
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
Cadence Guidelines

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information