Components of Regular Expressions

NI Requirements Gateway 2018 Help

Edition Date: March 2018

Part Number: 371714F-01

»View Product Info
Download Help (Windows Only)

Regular expressions can contain multiple components to accomplish the following tasks when Requirements Gateway analyzes intermediate files:

Matching Fixed Strings

Match a fixed string by writing the string itself in the expression. For example, the expression REQ_ matches the string REQ_ in REQ_1 and REQ_2.

Matching Special Characters

Cancel the special meaning of any special characters by inserting the backslash \ before the special characters which this topic mentions. For example, the expression \|2 matches |2 in the string |2 This is the requirement text. In regular expressions, a vertical bar specifies an alternation. However, in this example, the vertical bar matches itself because it follows a backslash.

Matching Character Sets

Use square brackets [] to create a character set, which allows you to match any one of a set of characters that you specify. For example, [abc] matches the letters a, b, and c.

Use a dash inside square brackets to specify a range of characters. For example, [a-z] matches any single lowercase letter.

Use a carat ^ inside square brackets to negate the character set. For example, [^\|] matches any single character except vertical bar.

You can also use the special expressions in the following table to capture variable characters.

Special Expressions Description
\d Matches a digit character, equivalent to [0-9]. For example, REQ\d matches REQ followed by one digit character, such as REQ0, REQ1, and so on.
\D Matches a non-digit character, equivalent to [^0-9].
\w Matches a word character, equivalent to [a-zA-Z0-9_].
\W Matches a non-word character, equivalent to [^a-zA-Z0-9_].
\s Matches a white space character, includes space, newline, tab, carriage return, and so on.
\S Matches a character, except white space character.
. Matches a single character except a link-break character.

Specifying Groups and Fields

Create a character group by using parentheses (). A quantifier that immediately follows a character group quantifies the entire group. For example, Refers to : REQ_\d? matches Refers to : REQ_ with or without following digit character, while Refers to : (REQ_\d)? matches Refers to :, or Refers to : REQ_ followed by digit character.

Requirements Gateway also uses parentheses to create a field. For example, if a requirement element specifies a regular expression (REQ\d+)\|2 ([^\|]+), the expression defines a (REQ\d+) field and a ([^\|]+) field. Requirements Gateway stores the text that matches a field. For example, ID (\d+) stores 45 from the match ID 45. You can also assign a field to capture specific information by right-clicking in the Regular expression control on the Analysis tab of the Types pane of the Configuration dialog box and selecting the information type to capture from the context menu.

To avoid capturing a field, add ?: to the beginning of the field. For example, the expression TestDuration=(\d+) ms - Result=(Passed|OK) captures the test duration value and the result value if the result value is Passed or OK. To capture only the test duration value but not the result value, use the expression TestDuration=(\d+) ms - Result=(?:Passed|OK).

Evaluating Occurrences

Match multiple characters by using the following quantifiers:

  • ?—Marks the single character, character set, or character group as one that can appear zero or one time. For example, Requirements? matches Requirement or Requirements.
  • +—Marks the single character, character set, or character group as one that can appear one or more time. For example, \d+ matches any number that contains one or several digit characters.
  • *—Marks the single character, character set, or character group as one that can appear zero or more time. For example, Requirement_\d* matches Requirement_, Requirement_1, Requirement_21, and so on.

Specifying Location

Identify the specific location in a string by using the following special characters:

  • ^—Anchors a match to the beginning of a line when you use it as the first character in an expression. For example, ^ent_ matches ent_ in ent_Text but not in requirement_Text.
  • $—Anchors a match to the end of a line when you use it as the last character in an expression. For example, end$ has a match in _end but not in end_.

Specifying Alternatives

Separate alternate possible matches by using a vertical bar |. For example, Priority = (High|Medium|Low) matches Priority = High, Priority = Medium, and Priority = Low.

Use the vertical bar inside parentheses because the alternate operator has the lowest precedence of any regular expression operator.

Matching Information from a Table

When Requirements Gateway converts tables into intermediate files, the exact syntax of the conversion varies according to the source files.

The following example shows a typical table that contains a requirement ID and requirement text.

Requirement ID Requirement Text
REQ1 This is the requirement text.

Converting Microsoft Word Tables

When Requirements Gateway converts a table in Word, the intermediate file formats the table information as |<column number> <word style> <Column content>, where the vertical bar | indicates the beginning of a table description.

The following figure shows the intermediate file when the example table is in a Word file.

To capture the requirement ID in the first column, specify an expression \|1 Normal\s(REQ\d+).

Converting Microsoft Excel Tables

When Requirements Gateway converts a table in Excel, the intermediate file formats the table information as |<column number> <Column content>, where the vertical bar | indicates the beginning of a table description.

The following figure shows the intermediate file when the example table is in a Excel file.

To capture the requirement ID in the first column, specify an expression \|1 (REQ\d+).

Capturing Multiple Lines

Use the expression [\s\S]* to capture information from multiple lines. In some default types, Requirements Gateway uses this expression to capture text.

Managing the Scope of Analysis

Restrict the scope of analysis by using a question mark.

The following table shows an example of an Excel table.

Requirement Value Priority Level Comment
REQ: Max Response Time 10 ms A
REQ: Max Output Level 6 V A 5 V +/-20%

To analyze only the requirement, you use the expression \|1(REQ:.+)\|. However, Requirements Gateway analyzes the information in the first three columns of the table. The intermediate file of this table contains several occurrences of the vertical bar (|). Requirements Gateways captures the information before the last vertical bar.

To restrict the analysis to the information in the first column of the table, you can use the expression \|1(REQ:.+?)\|. The question mark limits the analysis to only the first match of the vertical bar.


See Also

WAS THIS ARTICLE HELPFUL?

Not Helpful