Section A.1. Regular Expressions


[Page 595 (continued)]

A.1. Regular Expressions

Regular expressions are character sequences that describe a family of matching strings. They are accepted as arguments to many GNU utilities, such as grep, egrep, gawk, sed, and vim. Note that the filename substitution wildcards used by the shells are not examples of regular expressions, as they use different matching rules.

Regular expressions are formed out of a sequence of normal character and special characters. Figure A-1 is a list of special characters, sometimes called metacharacters, together with their meaning.

Figure A-1. Regular expression metacharacters.

Metacharacter

Meaning

.

Matches any single character.

[ ]

Matches any of the single characters enclosed in brackets. A hyphen may be used to represent a range of characters. If the first character after the [ is a ^, then any character not enclosed in brackets is matched. The *, ^, $, and \ metacharacters lose their normal special meaning when used inside brackets.

*

May follow any character, and denotes zero or more occurrences of the character that precedes it.

^

Matches the beginning of a line only.

$

Matches the end of a line only.

\

The meaning of any metacharacter may be inhibited by preceding it with a \.



[Page 596]

A regular expression matches the longest pattern that it can. For example, when the pattern "y.*ba" is searched for in the string "yabadabadoo", the match occurs against the substring "yabadaba" and not "yaba". The next page contains some examples of regular expressions in action.

To illustrate the use of these metacharacters, here is a piece of text followed by the lines of text that would match various regular expressions. The portion of each line that satisfies the regular expression is italicized.

A.1.1. Text

Well you know it's your bedtime, So turn off the light, Say all your prayers and then, Oh you sleepy young heads dream of wonderful things, Beautiful mermaids will swim through the sea, And you will be swimming there too. 


A.1.2. Patterns

Figure A-2 lists lines that match regular expression patterns.

Figure A-2. Lines matching regular expression patterns.

Pattern

Lines that match

the

So turn off the light,

Say all your prayers and then,

Beautiful mermaids will swim through the sea,

And you will be swimming there too.

.nd

Say all your prayers and then,

Oh you sleepy young heads dream of wonderful things,

And you will be swimming there too.

^.nd

And you will be swimming there too.

sw.*ng

And you will be swimming there too.

[A-D]

Beautiful mermaids will swim through the sea,

And you will be swimming there too.

\.

And you will be swimming there too. (the "." matches)

a.

Say all your prayers and then,

Oh you sleepy young heads dream of wonderful things,

Beautiful mermaids will swim through the sea,

a.$

Beautiful mermaids will swim through the sea,

[a-m]nd

Say all your prayers and then,

[^a-m]nd

Oh you sleepy young heads dream of wonderful things,

And you will be swimming there too.





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net