Home » Support » Tutorials/Power Tips » UltraEdit » Perl regular expressions: Digging deeper

IDM PowerTips

Perl Regular Expressions in UltraEdit: Digging Deeper

In our previous Perl regex power tip, we covered some the fundamentals of Perl regex Find and Replace in UltraEdit. If you're looking to expand your knowledge and harness the power of this popular and robust regular expression engine, take a deeper dive in this power tip.


Make dot "." span multiple lines

If you want your wildcard "." to span multiple lines and not stop at line breaks, then all you need to do is enable "single-line mode" by adding "(?s)" to the beginning of your regex string. For example, the following:

(?s)perl.*regex

...will match the highlighted portion of the following text:

In our previous Perl regex power tip, we covered some the fundamentals
of Perl regex Find and Replace in UltraEdit. If you're looking to
expand your knowledge and harness the power of this popular and robust
regular expression engine, take a deeper dive in this power tip.

If you want your wildcard "." to span multiple lines and not stop at
line breaks, then all you need to do is enable "single-line mode" by
adding "(?s)" to the beginning of your regex
string.


Using lookbehinds and lookaheads

Perl regular expressions include powerful features called "lookbehinds" and "lookaheads", which allow you to check the data in front of and behind a matched string of text, respectively, without including that data as part of your match. The syntax for lookbehinds and lookaheads is as follows:

(?<=a)b Positive lookbehind*; matches "b" when preceded by "a" ("a" is not included in the match)
(?<!a)b Negative lookbehind*; matches "b" when NOT preceded by "a"
b(?=a) Positive lookahead; matches "b" when followed by "a" ("a" is not included in the match)
b(?!a) Negative lookahead; matches "b" when NOT followed by "a"

* Important note: Because regular expressions cannot be applied backwards, lookbehinds must be fixed-width, meaning that you must specify the exact length of what to look behind for. For example, the following would be a valid lookbehind:

(?<=foo|bar)hello

...because both "foo" and "bar" are exactly 3 bytes in length, so the Perl regex engine knows to check the 3 bytes preceding the matched string. However, the following would not be a valid lookbehind:

(?<=f.*)bar

...because of the asterisk operator, which represents any number of the preceding character. The regex engine does not know how far back in the data to traverse to begin the lookbehind check. For this reason, it is not possible to use most wildcards and alternations when using lookbehinds.


Find all lines containing two different strings, regardless of order

To find all lines containing two strings regardless of which comes first, for example "cat" and "dog", use the following expression:

^(?:.*(?:cat).*(?:dog).*)|(?:.*(?:dog).*(?:cat).*)$

Note: the "(?:)" syntax is used for grouping purposes.


Find all lines containing one string, but not another

To find all lines containing one, for example "cat", but NOT another, for example "dog", use the following expression:

^(?!dog)(?:.(?!dog))*cat(?!dog)(?:.(?!dog))*$


Find a regex in a certain column

You can search for a particular pattern at a specific column position by using the following syntax:

(?<=.{39})f\w+bar

The above example will search for words at column 40 that begin with "f" and end with "bar"


Find characters based on their hex values

Search for a character by its hex value, or even a range of hex values, by using syntax similar to the following which demonstrates a search for non-printable ASCII characters:

[\x00-\x08\x0B\x0C\x0E\x0F\x10-\x1F\x7F]

This technique is used in the "Zap Gremlins" script on our extra downloads page.