Finding VISA Resources Using Regular Expressions

NI-VISA

Finding VISA Resources Using Regular Expressions

Using viFindRsrc() to locate a resource in a VISA system requires a way for you to identify which resources you are interested in. The VISA Resource Manager accomplishes this through the use of regular expressions, which specify a match for certain resources in the system. Regular expressions are strings consisting of ordinary characters as well as certain characters with special meanings that you can use to search for patterns instead of specific text. Regular expressions are based on the idea of matching, where a given string is tested to see if it matches the regular expression; that is, to determine if it fits the pattern of the regular expression. You can apply this same concept to a list of strings to return a subset of the list that matches the expression.

The following table defines the special characters and syntax rules used in VISA regular expressions.

Special Characters and Operators Meaning
? Matches any one character.
\ Makes the character that follows it an ordinary character instead of special character. For example, when a question mark follows a backslash (\?), it matches the ? character instead of any one character.
[list] Matches any one character from the enclosed list. You can use a hyphen to match a range of characters.
[^list] Matches any character not in the enclosed list. You can use a hyphen to match a range of characters.
* Matches 0 or more occurrences of the preceding character or expression.
+ Matches 1 or more occurrences of the preceding character or expression.
exp|exp Matches either the preceding or following expression. The OR operator | matches the entire expression that precedes or follows it and not just the character that precedes or follows it. For example, VXI|GPIB means (VXI)|(GPIB), not VX(I|G)PIB.
(exp) Grouping characters or expressions.

The priority, or precedence of the operators in regular expressions is as follows:

  • The grouping operator () in a regular expression has the highest precedence.
  • The + and * operators have the next highest precedence.
  • The OR operator | has the lowest precedence.

Notice that in VISA, the string "GPIB?*INSTR" applies to both GPIB and GPIB-VXI instruments.

The following table lists some examples of valid regular expressions that you can use with viFindRsrc().

Regular Expression Sample Matches
?*INSTR Matches all INSTR (device) resources.
GPIB?*INSTR Matches GPIB0::2::INSTR, GPIB1::1::1::INSTR, and GPIB-VXI1::8::INSTR.
GPIB[0-9]*::?*INSTR Matches GPIB0::2::INSTR and GPIB1::1::1::INSTR but not GPIB-VXI1::8::INSTR.
GPIB[^0]::?*INSTR Matches GPIB1::1::1::INSTR but not GPIB0::2::INSTR or GPIB12::8::INSTR.
VXI?*INSTR Matches VXI0::1::INSTR but not GPIB-VXI0::1::INSTR.
GPIB-VXI?*INSTR Matches GPIB-VXI0::1::INSTR but not VXI0::1::INSTR.
?*VXI[0-9]*::?*INSTR Matches VXI0::1::INSTR and GPIB-VXI0::1::INSTR.
ASRL[0-9]*::?*INSTR Matches ASRL1::INSTR but not VXI0::5::INSTR.
ASRL1+::INSTR Matches ASRL1::INSTR and ASRL11::INSTR but not ASRL2::INSTR.
(GPIB|VXI)?*INSTR Matches GPIB1::5::INSTR and VXI0::3::INSTR but not ASRL2::INSTR.
(GPIB0|VXI0)::1::INSTR Matches GPIB0::1::INSTR and VXI0::1::INSTR.
?*VXI[0-9]*::?*MEMACC Matches VXI0::MEMACC and GPIB-VXI1::MEMACC.
VXI0::?* Matches VXI0::1::INSTR, VXI0::2::INSTR, and VXI0::MEMACC.
?* Matches all resources.

Notice that in VISA, the regular expressions used for resource matching are not case sensitive. For example, calling viFindRsrc() with "VXI?*INSTR" would return the same resources as invoking it with "vxi?*instr".