Download BBEdit 8.2 User Manual

Transcript
BEUM book Page 137 Tuesday, April 19, 2005 11:47 AM
What if we wanted to match a series of digits, followed by a plus sign, followed by the
exact same series of digits as on the left side of the plus? In other words, we want to
match “1234+1234” or “7+7”, but not “5432+1984”.
Using grouping parentheses, you can do this by referring to a backreference, also
known as a captured subpattern. Each set of parentheses in the pattern is numbered
from left to right, starting with the opening parenthesis. Later in the pattern, you can
refer to the text matched within these backreferences by using a backslash followed by
the number of the backreference.
Pattern
Matches
Examples
(\d+)\+\1
a string of digits, followed by
a plus sign, followed the
same digits
7+7
1234+1234
(\w+)\s+\1
double words
the the
(\w)(\w)\2\1
a word character, a second
word character, followed by
the second one again and the
first one again
abba
We will revisit subpatterns in the section on replacement, where you will see how the
choice of subpatterns affects the changes you can make.
Using Alternation
The alternation operator | allows you to match any of several patterns at a given point.
To use this operator, place it between one or more patterns x|y to match either x or y.
As with all of the preceding options, you can combine alternation with other pattern
elements to handle more complex searches.
Pattern
Text is…
Matches…
a|t
A cat
each “a” and “t”
a|c|t
A cat
each “a”, “c”, and “t”
a (cat|dog)
is
A cat is here. A dog is
here. A giraffe is here.
“A cat is”, “A dog is”
A|b+
Abba
“A”, “bb”, and “a”
Andy|Ted
Andy and Ted joined AAA
yesterday
“Andy” and “Ted”
\d{4}|years
I’ve been a loyal member
since 1983, almost 16
years ago.
“1983”, “years”
[a-z]+|\d+
That’s almost 16 years.
“That”, “s”, “almost”,
“16”, “years”
Writing Search Patterns
137