Examples – Finding Text

These examples all use UNIX extended regular expression syntax.

1. Find all trailing spaces:

[ \t]+$
finds one or more spaces or tabs followed by the end of line.

2. Find an empty line:

^$
finds the beginning of a line immediately followed by its end.

3. Find everything on a line:

^.*
finds the beginning of a line, followed by zero or more of any characters, up to the end of the line.

4. Find “$12.34”:

\$12\.34
Note that ‘.’ and ‘$’ have been escaped using the backslash to hide their regular expression meanings.

5. Find any valid C language variable name:

\<[ a-zA-Z][ a-zA-Z0-9]*
finds a word starting with an underscore or alphabetic character, followed by zero or more underscores or alphanumeric characters.

6. Find an inner-most bracketed expression:

([^()]*)
finds a left bracket, followed by zero or more characters excluding left and right brackets, followed by a right bracket.

7. Find a repeated expression:

\([0-9]+\)-\1
This uses a tagged expression “\(…\)” to find one or more digits, followed by a hyphen, followed by the string matched by the tagged expression. So this regular expression will find 12-12, but not 12-34.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: