Unix Power ToolsUnix Power ToolsSearch this book

32.9. Regular Expressions: Exceptions in a Character Set

You can easily search for all characters except those in square brackets by putting a caret (^) as the first character after the left square bracket ([). To match all characters except lowercase vowels, use [^aeiou].

Like the anchors in places that can't be considered an anchor, the right square bracket (]) and dash (-) do not have a special meaning if they directly follow a [. Table 32-2 has some examples.

Table 32-2. Regular expression character set examples

Regular expression

Matches

[0-9]

Any digit

[^0-9]

Any character other than a digit

[-0-9]

Any digit or a -

[0-9-]

Any digit or a -

[^-0-9]

Any character except a digit or a -

[ ]0-9]

Any digit or a ]

[0-9]]

Any digit followed by a ]

[0-99-z]

Any digit or any character between 9 and z

[ ]0-9-]

Any digit, a -, or a ]

Many languages have adopted the Perl regular expression syntax for ranges; for example, \w is equivalent to "any word character" or [A-Za-z0-9_], while \W matches anything but a word character. See the perlre(1) manual page for more details.

-- BB



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.