Full Page History:

Oldest known version of this page was edited on 2006-01-09 11:01:34 by OswaldCz? []

Page view:

Regular Expressions

RegExp, also known as regex or Regular Expressions, is a specialised string which can “match” text, and can also do a more advanced “search and replace” than normal string matching. It uses characters with special meaning to allow very flexible matching of strings.

Some examples:

abc will find any “abc” in text, like: aksjabcskabbcldga, abcasfreabababcacas

ab*c will find a letter ‘a’, followed by zero or more letters ‘b’ and one letter ‘c’, like these: fdkhjaclsdabcsldkabbbbbbbcksdd

a[bde]c will find a letter ‘a’, followed by ‘b’ or ‘d’ or ‘e’, followed by ‘c’, like: skdjacacabcaabbccadcandaecacasce

a[bde]*c will find a letter ‘a’, followed by zero or more letters ‘b’ or ‘d’ or ‘e’, followed by ‘c’, like these: asdljacandaaabdebdbbdecoppabbbbcbutalsoadcandsoon

It can be more complicated and may also act as a “search and replace”. Look:

s/\([^,]*\), \([^,]*\)/\2 \1/ (using default syntax of Vim and sed) - In a file with each line containing a name in form “Smith, John”, this RegExp, when applied to each line, converts the names to “John Smith” form.

More information:

See the Wikipedia page for a more thorough description.

Windows only -> To learn how to use regular expressions interactively, use the wonderful regular expression coach.

Linux only -> You may also try the ^txt2regex$, which is a Regular Expression “wizard”, all written with bash2 builtins, that converts human sentences to RegExps.

Free software with support to RegExp:

Opera (in M2 filters and in JavaScript scripts)
Vim, a very powerful but a bit difficult to learn text editor (multi-systems)
ReplaceEm (Windows)
Lots of Unix/Linux utilities (sed, awk, locate, grep, egrep…)

Category

CategoryTechnical

Back Links