Regular Expressions
Regular Expressions
Regular expressions are special search strings that are useful for matching complicated patterns. In a regular expression string, many characters have special meanings. For example, there is a special character that means "the beginning of the line".
Optional Syntaxes
Source Insight support two different regular expression syntaxes: "Source Insight" and "Perl Compatible".
Most places that let you enter a regular expression has a down-down list where you can select the regular expression syntax.
Multi-Line Patterns
There are regular and "multi-line" versions of each regular expression syntax. The multi-line versions basically match a new-line (end-of-line sequence) with the dot (.) character. So for example,
.*
would match the whole file. And
"start.*end"
would find everything from "start" to "end" in the file, even across line breaks.
Regular Expression Elements
Wildcard Matching
. (dot)
The dot . matches any character.
Example
: b.g
matches big, beg, and bag, but not bp or baag.
If you use the "multi-line" version of the regular expression syntax, then the dot (.) character also matches new-lines. For example
.*
matches the whole file.
Matching the Beginning or End of a Line
^ and $
The caret
^
matches the beginning of a line when the caret appears as the first character in the search pattern.
Example
:
^Hello
matches only if Hello appears at the beginning of a line.
The
$
matches the end of a line.
Example
:
TRUE$
matches only if TRUE appears at the very end of a line.
Matching a Tab or Space
\t
\s
\w
\t
matches a single tab character.
Example
:
\tint abc;
matches a tab character followed by int abc;.
\s
matches a single space character.
Example
:
\sif
matches a space character followed by if.
\w matches a single white space character. In other words, \w matches either a tab or space character.
Example
:
\wwhile
matches either a tab or space character, followed by while.
Source Insight 4 User Guide 107
Regular Expressions
Matching 0, 1, or More Occurrences
* and +
*
matches zero or more occurrences of the preceding character. The fewest possible occurrences of a pattern will satisfy the match.
Example
: a*b
will match b, ab, aab, aaab, aaaab, and so on.
+
matches one or more occurrences of the preceding character.
Example
: a+b
will match ab, aab, aaab, aaaab, and so on, but not just b.
Matching Any in a Set of Characters
[ .. ]
When a list of characters are enclosed in square braces [..] then any character in that set will be matched.
Example
:
[abc]
matches a, b, and c, but not d.
When a caret ^ appears at the beginning of the set, the match succeeds only if the character is not in the set.
Example
:
[^abc]
matches d, e, or f, but not a, b, or c.
Sets can conveniently be described with a range. A range is specified by two characters separated by a dash, such as
[a-z]
. The beginning character must have a lower ASCII value than the ending character.
Example
:
[a-z]
matches any character in the range a through z, but not A or 1 or 2.
Sets can contain multiple ranges.
Example 1
:
[a-zA-Z]
matches any alphabetic character.
Example 2
:
[^a-zA-Z0-9]
matches any non-alphanumeric character.
Matching a Line Break
\n
This matches a new-line, or line-break. Use this when you want to match an end-of-line within a larger pattern.
Example
: dog\ncat
matches dog, followed by a line break, followed by cat.
Regular Expression Groups
\( and \)
Parts of a regular expression can be isolated by enclosing them with
\(
and
\)
, thereby forming a group.
Groups are useful for extracting part of a match to be used in a replacement pattern. Each group in a pattern is assigned a number, starting with 1, from left to right.
Example
: abc\(xyz\)
matches abcxyz. xyz is considered group #1.
This is not all that useful, unless we are using the Replace command. The replace string can contain group characters in the form of \<number>. Each time a group character is encountered in the replacement pattern, it means "substitute the group value from the matched pattern".
Example 1
: replace
\(abc\)\(xyz\)
with
\2\1
. This replaces the matched string abcxyz with the contents of group #2 xyz, followed by the contents of group #1 abc. So abcxyz is replaced with xyzabc. This is still not too amazing. See the next example.
Example 2
: replace
\(\w+\)\(.*\)ing
with
\1\2ed
. This changes words ending in ing with the same word ending with ed. Your English teacher would not be too happy.
Overriding Regular Expression Characters
\ (backslash)
A backslash character \ preceding a meta-character overrides its special meaning. The backslash is ignored from the string.
108 Source Insight 4 User Guide
Regular Expressions
Example
: a\*b
matches a*b literally. The * character does not mean "match 0 or more occurrences".
Multi-Line Matches
Most places in the program where you can specify a regular expression also has a "regular expression syntax" selection. If you select the "multi-line" version of the syntax, then the dot (.) character matches new-lines. For example
.*
matches the whole file.
Example
: begin.*end
matches everything from "begin" to "end" across multiple lines. "begin" could be at line 1 and "end" could be at line 100.
Regular Expression Summary
The following special characters are interpreted in regular expressions
\t
\s
\w
[abc]
[^abc]
*
+
\n
$
Table 3.7: Regular Expression Characters
Character
^ (at the beginning only)
.
Matches
beginning of line any single character. In multi-line mode, this also matches the endof-line character.
any single character that belongs to the set abc any single character that does not belong to the set abc zero or more occurrences of the preceding character one or more occurrences of the preceding character a tab character a space character white space (a tab or a space character) new-line, or the end-of-line character. Use this if you want to match a line break in the middle of a pattern.
the end of the line
Sets, such as [abc] may be in the following formats.
Set type
[<character list>] eg.
[abcde]
[x-y] eg.
[a-z] combination; eg.
[WXYa-z0-9]
Table 3.8: Regular Expression Sets
Meaning
Matches any character within the set. The set can be any number of characters long.
Matches on any character within the range of x through y, inclusively. The ASCII value of x must be less than that of y.
Character lists and ranges may be combined.
Source Insight 4 User Guide 109