RegExp.com
Wide range of in-depth information
                Themes: Default |White |Blue |Green |Orange
                Font Size: Default |Small |Medium |Large

Common Examples

The following examples may be useful, they are particulary aimed at extracting parameters but cover some other ground as well. If anyone wants to email us some more examples we'll be happy to post with an appropriate credit.

# split on simple space
string "a b c"
re = \S+
result = "a", "b", "c"

# css definition split on space or comma but keep "" enclosed items
string = '10pt "Times Roman", Helvetica,Arial, sans-serif'
re = \s*("[^"]+"|[^ ,]+)
result = "10pt" ,"\"Times Roman\"", "Helvetica" ,"Arial", "sans-serif"

# extract HTML <> enclosed tags
string = 'A link'
re = <[^>]*>
result = '', ""

# separate tag and attributes from an HTML tag
string = ''
re =
result = "a", 'href="#", 'class="link"'


Utility and Language Notes - General

Certain utilities, notably grep, suggest that it is a good idea to enclose any complex search expression inside single quotes. In fact it is not a good idea - it is absolutely essential! Example:

grep 'string\\' *.txt # this works correctly grep string\\ *.txt # this does not work

Some utilities and most languages use / (forward slash) to start and end (de-limit or contain) the search expression others may use single quotes. This is especially true when there may be optional following arguments (see the grep example above). These characters do not play any role in the search itself.

Utility Notes - Using Visual Studio

For reasons best know to itself VS (VS.NET) uses a bizarre set of extensions to regular expressions. MS documentation but there is a free regular expression add-in if you want to return to sanity.

Utility Notes - Using sed

Stream editor (sed) is one of those amazingly powerful tools for manipulating files that are simply horrible when you try to use them - unless you get a buzz out of ancient Egyptian hieroglyphics. But well worth the effort. So if you are hieroglyphically-challenged, like us, these notes may help. There again they may not. There is also a useful series of tutorials on sed and this list of sed one liners.

not all seds are equal: Linux uses GNU sed, the BSDs use their own, slightly different, version.

sed on windows: GNU sed has been ported to windows.

sed is line oriented: sed operates on lines of text with the file or input stream.

expression quoting: To avoid shell expansion (in BASH especially) quote all expressions in single quotes as in a 'search expression'.

sed defaults to BRE: The default behaviour of sed is to support Basic Regular Expressions (BRE).

To use all the features described on this page set the -r (Linux) or -E (BSD) flag to use Extended Regular Expressions (ERE) as shown:

# use ERE on Linux (GNU sed)
sed -r 'search expression' file
# use ERE on BSD
sed -E 'search expression' file

in-situ editing: By default sed outputs to 'Standard Out' (normally the console/shell). There are two mutually exclusive options to create modified files. Send the text to a file or use in-situ editing with the -i option.

The following two lines illustrate the options:

# saves the unmodified file to file.bak BEFORE
# modifying
sed -i .bak 'search expression' file

# file is UNCHANGED the modified file is file.bak
sed 'search expression' file > file.bak



First |  Next |  Previous |  Last