Regular expressions

The regular expressions that you can use in the search and replace dialogs and in places like the 'Feature' attributes, the 'Primary index' of an 'Item' node or in checks all use standard Java regexp syntax. Before QF-Test version 3.1 the default was the GNU regexp package (see also appendix F). It can still be activated via the option Use old-style GNU regexps (before 3.1). One major difference between the two is that '{' and '}' are special characters that need to be escaped with '\' for Java regexps, but normal characters for GNU:

Detailed regexp documentation with links to further information and even a whole book about regular expressions are provided in the Java documentation for the class java.util.regex.Pattern at http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html. It's also worth to have a look at the Wikipedia article about regular expressions.
Following is a short summary of the basics:

  • A '.' stands for any one character except line breaks. With the new Java regexps you can start your regexp with the embedded flag '(?s)' to treat multi-line text like a single line so '.' will match everything. The old GNU regexps have no such flag, so you need to use '(.|\n)' to match everything. Unfortunately this expression causes a StackOverflowException with Java regexps, so if QF-Test finds this expression in any regular expression it will treat it as a GNU regexp regardless of the option setting.
  • Character in between '[' and ']' match any one of these characters.
  • A '?' says the preceding element is optional, i.e. it may appear 0 or 1 times.
  • '+' means at least one of the preceding element.
  • '*' means 0 or more of the preceding element.
  • A group is created with '(' and ')'. A '?', '+' or '*' after the closing brace refers to the whole group. All groups in a regexp are numbered in the order of their opening brace. The first group has the number 1, 0 stands for the whole regexp. For search and replace, $n in the replacement string expands to the part of the original value matched by the nth group. Example: To change the extension of file names starting with /tmp/ from .foo to .bar, search for (/tmp/.*)\.foo and replace with $1.bar.
  • A '|' separates alternatives in a group.
  • '\' quotes to suppress the special meaning of the following character or introduces special characters, e.g. '\n' for LineFeed (a line break), '\r' for CarriageReturn (not needed for QF-Test, see section 47.4) or '\t' for Tab.

Examples:

  • .* describes a sequence of arbitrary characters, which is optional.
  • .+ describes a sequence of arbitrary characters, but there must be at least one character, i.e. some mandatory characters.
  • [0-9] describes one arbitrary cipher.
  • [0-9]+ describes a sequence of arbitrary ciphers, but there must be at least one cipher.
  • [0-9]{1,3} describes a sequence of arbitrary ciphers, but there must be at least one cipher and at maximum three ciphers.
  • To match any text that contains the word 'tree' use '.*tree.*'.
  • To match arbitrary text possibly including line breaks: '(?s).*' for Java regexps and '(.|\n)*' for GNU.
  • To replace 'tree' in arbitrary text with 'node' use '(.*)tree(.*)' to search and $1node$2 to replace. In the replace dialog simply replace tree with node and disable the "Match whole string" check box to achieve the same effect.
  • To search for 'name' or 'names': 'names?'
  • To search for 'tree' or 'node': '(tree|node)'
  • An arbitrary word consisting of letters and numbers: [0-9a-zA-Z]+
  • ...

4.0+ QF-Test allows you to use the context menu item »Escape text for regular expressions« on all attributes which allow regular expressions in order to escape special characters of regular expressions correctly with '\'.