Regular Expressions In CadEnhance Tools

Many of the CadEnhance Tools require the user to enter Regular Expressions as pattern matches for strings or filenames.  For example, PartBuilder uses them for multiple purposes including:

  • Matching pins in the SYMBOL_DESCRIPTION_LANGUAGE,
  • Renaming signals with the SIG_RENAME_RULES
  • Assigning pinTypes to matching pinNames
  • Searching and Replacing Text in the SDL Editor Search/Replace Function
  • Filtering displayed pins by PinName, PinType, and Pin Number in The Pin Explorer Match Fields
  • Identifying DiffPair members

Most computer users have used wildcard matching when they are searching for a file matching "*.txt" or "*.*" or "test?_*.pdf". 

The simple definition of A Regular Expression is "a string that defines a pattern match", but the best way to describe Regular Expressions is a wildcard on steroids.

Most people working with Regular Expressions call them regex or regexes. Regular Expressions enable the user to search a string for a much more specific pattern match than a wildcard would allow

Regular Expressions are an extremely useful tool for an Engineer, Designer or Librarian to learn to use. 

Users familiar the 'grep' tool which searches files or strings for patterns know that the pattern they provide is a Regular Expression.

In the simplest case a regular expression can just be a simple string to match like 'red'  which would instruct the matcher to look for any string that contains red

Quick Tips

-use '.*' instead of '*'

If you have used a wildcard to look for a string or file like 'test*' to find files named: test_1.txt test_blue.dat, test_green_23.xls,

the equivalent regular expression for this is 'test.*' 

In this regex, the '.' means any character and the '*' is a quantifier for the preceeding '.'  which tells the matcher to look for any number of '.' (which matches any character), including zero.

Another common quantifier is '+' which matches when it sees at least one of the preceeding characters (in this case the user would enter 'test.+'

In most cases the CadEnhance tools will convert the '*' to '.*' for you, but it can sometimes cause confusion

Special Regex Characters

There are a handful of characters which have special meaning when used in a regular expression match. These include:

^ . *  +  ? [ ] ( ) $ 

If you want to look for them directly in a string, you will need to 'ESCAPE' them by adding a backslash '\' before them in the regular expression match

\^ \. \* \? \[ \] \( \( \$

So If you were looking for the equivalent wildcard for 'test*_.txt' where the '.' character has to be present, you would need to enter it as 'test.*_\.txt'


Tutorials 

Excellent references for Regular Expressions exist on the web like with interactive the one at this link: [ Info on regular expressions  ]



Examples

You can also use tools like PartBuilder's PinExplorer to play with regular expression patterns to find pins with matching  pinNames or pinTypes 

Here are some links that provide working examples of regular expressions used in specific contexts:

LocationDescription
PIN_MATCH Examplesregexs used as PIN_MATCHES in SDL
SIG_RENAME_EXAMPLESregexs used to rename pins
Regular Expression Rename Rules DiffPair Renaming... Example 1This page shows how regexs are used to rename diffPairs
PIN_TYPE_OVRD Fileshows regex used to define pinTypes efficiently


Bus-Enhanced Regular Expressions.

Standard Regular expressions do not work very well with busses which are an integral part of schematics and board design.

The cadEnhance tools do some behind the scenes work to modify Regular Expressions to work better with busses

Its not truly necessary to understand this detail, but In a normal regular expression, the '[]' characters in a pattern match define a character class which is simply a list of characters to match.

For example, in PartBuilder, If you were comparing a list of pins dq7 dq6 dq5 dq4 dq3 dq2 dq1 and  dq0 used a  standard regular Expression 'dq[0:7]', the pattern would only match dq0 and dq7 (and funny enough dq:).

You could get more useful results with 'dq[0-7]' (because 0-7 inside the square brackets says to match any character between 0 and 7) but unfortunately that would not enforce the order of the pins. 

To work around this issue, when PartBuilder sees the 'DQ[0:7]' in a PIN_MATCH regex, it  actually expands it to 8 individual regexes: DQ0, DQ1, DQ2... DQ7 which are then used as pattern matches.

It also maintains the order of the bits so that they will be added to the symbol in LSB→MSB order if you enter busName[0:7] or MSB→LSB order if you enter busName[7:0]