Slick V3.3 manual Minimal versus Maximal Matching

Models: V3.3

1 568
Download 568 pages 2.03 Kb
Page 217
Image 217

 

Find and Replace with Regular

 

Expressions

 

 

 

Example

 

Description

 

 

 

UNIX regular expression:

 

Replace occurrences of "if" and "while" with "xify"

Search for: (ifwhile)

 

and "xwhiley." Unmatched groups are null. Note:

Replace with: x\1y\2

 

The UNIX syntax \2 (SlickEdit syntax #1, Brief syn-

SlickEdit® regular expression:

 

tax \1) is replaced with null.

 

 

Search for: {ifwhile}

 

 

Replace with: x#0y#1

 

 

Brief regular expression:

 

 

Search for: {{if}while}}

 

 

Replace with: x\0y\1

 

 

UNIX regular expression:

 

Reverse text on lines containing a comma. Lines

Search for: ^(.*?),(.*)$

 

with "abc,def" will be changed to "def,abc." Notice

Replace with: \2,\1

 

that the UNIX regular expression search string uses

SlickEdit regular expression:

 

a minimal matching operator *? so that the comma

 

actually matches the first comma in the line and not

Search for: ^{?*},{?*}$

 

the last.

Replace with: #1,#0

 

 

Brief regular expression:

 

 

Search for: ^{*},{*}$

 

 

Replace with: \1,\0

 

 

Minimal versus Maximal Matching

If you are using tagged expressions or regular expressions to perform a search and replace, you need to understand the difference between the minimal and maximal operators.

Take, for example, a line of text which contains a DOS file name: \path1\path2\path3\name.ext.

The regular expression ^\\.*?\\ (UNIX), ^\\?*\\ (SlickEdit® ), or <\\*\\ (Brief) will match the string \path1\.

The regular expression ^\\.*\\ (UNIX), ^\\?@\\ (SlickEdit), or <\\\:*\\ (Brief), which uses the maximal oper- ator, matches the string \path\path2\path3\.

As a rule of thumb, you will usually want to use the minimal matching operators *? (UNIX), * (SlickEdit), or

@(Brief) and +? (UNIX), + (SlickEdit/Brief) after a less-specific regular expression such as . (UNIX) or ? (SlickEdit/Brief).

You will usually want to use the maximal matching operators after a regular expression which matches something more specific. For example, to search for a string of digits and prefix each string of digits with the character $, you would specify the following in the Replace tab of the Find and Replace view:

UNIX regular expression: Search for: ([0-9]+) Replace with: $\1

195

Page 217
Image 217
Slick V3.3 manual Minimal versus Maximal Matching, Unix regular expression Search for 0-9+ Replace with $\1