Regular Expressions for Searching Text
Regular expressions consist of patterns that can be used to search for variable forms of text. Without regular expressions, you must know the exact phrase for which to search; regular expressions allow you to find all text that follows a certain pattern. Even if the Regular Expressions check box in the Find/Replace dialog is on, you can still search for exact phrases. In such a case, you have to remove the regular expression operators from the phrase (see Syntax).
Syntax of Regular Expressions for Searching Text
Regular expressions generally consist of some constant text, and some restrictions or wildcards on the rest of the text. If you want to use one of these operators as literal text, you must precede the character with a backslash.
The following is a list of regular expressions used only for searching text. When searching text, you can also use regular expressions that are used for all types of operations.
Character | Meaning |
---|---|
[ ] | Characters within these brackets describe a character class, or a custom wildcard. |
* | The preceding character (or character class) is allowed to repeat zero. |
+ | The preceding character (or character class) must repeat at least once. |
- | When used within a character class, represents a character range. |
Examples of Regular Expressions for Searching Text
- Find a line that starts with "A" and ends with "2":
- Find text that contains the word "Plan," followed by optional space and a 4-digit number:
- Find text that contains the characters ' [# ' but is not followed by a 1, 2, 3, 4, or B:
\[#[^1-4B] \[ The literal character [ (must be escaped) # The literal character # (not an operator, does not need to be escaped) [^ Starts a character class, meaning any character but those described in the class 1-4B Range 1-4 (1, 2, 3, 4) and B ] Ends the character class Samples Part [#9778C] Found Lot [#554] Found Part #977 Not found (# not preceded by [) Part [#155A] Not found (# followed by 1) - Find a line that starts with "Detail," followed by one or more spaces, followed by one or more alphanumeric characters, and a Z:
- Find currency values in the form $#,###.## :
\$[0-9,]+\.:d:d \$ Match must start with a dollar sign [0-9,]+ Matches one or more digits or commas \. Followed by a decimal point :d:d Followed by exactly two digits Samples: $12.34 Found $12,123.00 Found 12,435.00 Not found (does not start with $) $12 Not found (does not end with a decimal point and two digits)