There exist several definitions / standards / implementations for regeps. They share a common core. The most popular ones are (see the Wikipedia article for details.
- POSIX Basic Regular expressions (BRE)
- POSIX Extended Regular expressions (ERE)
- Perl-derivative regular expressions
2.1 Removing HTML code
Identifies both img and a begin tags ([http://stackoverflow.com/questions/3790681/regular-expression-to-remove-html-tags StackOverflow)
Removes span (begin tag)
2.2 Zip code
The following defines a somewhat legal Swiss Zip code:
The following one defines a valid email address (example from http://www.regular-expressions.info/email.html)
2.3 Replacing Wiki text
Administrators of this wiki can use RegExps to make mass changes to pages.
The example from the french version below shows how to replace
- [[Flash CS4 - Composant bouton]]/
- [[Flash CS5 - Composant bouton]] ([[Flash CS4 - Composant bouton|CS4]])
Note how we had to quote the [[ ]]
- /\[\[Flash CS4 - Composant bouton\]\]/
Removing pageby + args tag
/<pageby nominor="false" comments="false"\/>/
Search for (a bit dangerous):
3 Mass editing HTML files with Perl
Let's assume that you would like to add some line after each <body .....> tag.
Let's find all files that have body tags:
find . -type f -print | xargs grep -l "<body\(.*\)>"
Replace (that's more hairy)
find . -type f -print | xargs perl -i~ -pe "s:<body(.*)>:<body \\1> <p>Something new</p>:g"
- -pe means to execute a one liner Perl command (i.e. the expression within the " .....")
- -i~ means to replace the orginal file, but create a backup copy with a "~" appended
- The pattern s/search regexp/replacement/g defines a search/replace pattern
- Since we got a / in the closing p tag, we will use ":" to separate the two
- The final :g means to replace all occurences (not needed in our case actually).
- The grouping () don't need to be escaped in Perl. I hate regexps :)
- Most good text editors provide regexp support
- Regular expression (Wikipedia)
- regular-expressions.info Includes a catalogue of most popular ones)
- Using Regular Expressions by Stephen Ramsay, Electronic Text Center, University of Virginia]
- The 30 Minute Regex Tutorial By Jim Hollenhorst
5.2 Programming languages
- Introduction to Regular Expressions (microsoft)
- Some explanations for a very simple chatter bot
- Regular Expressions Cheat Sheet by LoveJackDaniels.com / Dave Child.
5.4 Online tools
- regexpal.com (A testing tool for regexps)