The Regex Coach
Content formatting with Regexes
The Regex Coach a graphical application for Linux and Windows which can be used to experiment with (Perl-compatible) regular expressions interactively.
With this tool you don't have to be a master at creating regular expressions. You can interactively create a regular expression and preview the results instantly. It shows which parts of the target string correspond to parts of the regular expression. The program highlights the matches it finds for your expression, meaning that you can see the how your results change as you modify your expression.
A short regular expression refresher:
A caret (^) may be used to indicate the beginning of the string
A dollar sign ($) is used to indicate the end
PHP // Matches "What is PHP?“
^PHP // Matches "PHP rules!" but not "What is PHP?“
PHP$ // Matches "I love PHP" but not "What is PHP?“
^PHP$ // Matches "PHP" but nothing else
To use ^, $, or other special characters as regular characters in a search string prefix it with a backslash:
\$\$\$ // Matches “Show me the $$$!”
Other cases:
\| // Vertical bar
\[ // An open square bracket
\) // A closing parenthesis
\* // An asterisk
\^ // A carat symbol
\/ // A slash
\\ // A backslash
The characters ?, +, and * also have special meanings. ? means "the preceding character is optional", + means "one or more of the previous character", and * means "zero or more of the previous character".
bana?na // Matches "banana" and "banna", but not "banaana".
bana+na // Matches "banana" and "banaana", but not "banna".
bana*na // Matches "banna", "banana", and "banaaana", but not "bnana".
^[a-zA-z]+$ // Matches any string of one or more letters and nothing else.
Parentheses () may be used to group strings together to apply ?, +, or * to them as a whole.
ba(na)+na // Matches "banana" and "banananana", but not "bana" or "banaana".
The period (.) matches any character except a newline
^.+$ // Matches a newline character
Content Formatting
Regular Expressions are ideal for content formatting. You can implement your own content formatting method by creating a markup language. Use regexes to search for custom markup tags in text and replacing them with their HTML equivalents before outputting the text to the user's browser.
ereg_replace accepts a regular expression and a string of text and attempts to match the regular expression in the string. Also takes a second string of text, and replaces every match of the regular expression with that string (eregi_replace is case insensitive):
$newstring = ereg_replace(<regexp>, <replacewith>, <oldstring>);
<regexp> is the regular expression, is the string that will replace matches to <regexp> in <oldstring>. The function returns the new string that is the outcome of the replacement operation. In the above, this gets stored in $newstring.