Regular Expression Examples
Below are given some examples of using regular expressions in various
situations.
Find any HTML colour constant.
| Find pattern |
\#{[0-9A-Fa-f]}+ |
Replace all HTML colour constants with the new one (#AFEEEE).
| Find pattern |
\#{[0-9A-Fa-f]}+ |
| Replace pattern |
#AFEEEE |
Replace all occurences of full stop before the lowercase letter with comma,
truncating separating spaces to only one (useful for scanned documents).
| Find pattern |
{\.\s#}(\L[a-z]) |
| Replace pattern |
\, \1 |
Strip HTML tags.
| Find pattern |
{\<}{\/?}{.#}{\>} |
| Replace pattern |
empty |
Change the value of all "background-color:" CSS properties
everywhere.
| Find pattern |
(background\-color\:){\#{[0-9A-Fa-f]}+} |
| Replace pattern |
\1#FFE4E1 |
Replace <P> tags with <DIV> tags, preserving tag attributes.
| Find pattern |
\<P\s(.#)\>(.#)\<\/P\> |
| Replace pattern |
<DIV \1>\2</DIV> |
Colorize HTML tags for use inside HTML page: symbols with blue, tag with red, convert < and > to entities.
| Find pattern |
\<({\/}?)(.#)\> |
| Replace pattern |
<span style="color:blue"><\1</span><span style="color:red">\2</span><span style="color:blue">></span> |
Find host addresses.
The pattern would match the following:
- http://www.host.dom/
- http://host.dom/
- ftp://203.131.69.75/
- www.host.dom/
| Find pattern |
({ht}|{f}tp\:\/\/)?{[0-9]#\.[0-9]#\.[0-9]#\.[0-9]#}|{{[^\.\/]#\.}?{[^\.\/]#\.[^\.\/]#}}\/ |
| Replace pattern |
|
Find the whole paragraph; store the paragraph contents.
| Find pattern |
(.#){\r\n}|{{\r}|{\n}} |
| Replace pattern |
|