This page shows examples of the rred regular expression debugger in use. If these examples make sense to you, then you might find rred useful. If not, it's safe to assume you don't need rred :)
hr@moonshine:~$ rred
rred regular expression debugger version 0.1
Commands:
t<text> Set text to match against
e<regexp> Set regular expression
m<text> Set match modifiers (default is 'g')
q<char> Set text quotes (default is '"')
d<char> Set expression delimiters (default is '/')
x, ^D exit
rred>
This example is going to demonstrate a regexp to find five-letter words. First, we enter the regexp:
rred> e\b\w{5}\b
Initially, we just get the regexp echoed back at us:
$text = ""; $text =~ m/\b\w{5}\b/g;
rred>
To see if it works, we enter some text to match against:
rred> tRusty old river, rusty old boat.
The output shows us what parts of the string match our regexp:
$text = "Rusty old river, rusty old boat."; $text =~ m/\b\w{5}\b/g;
Rusty old river, rusty old boat.
$& ^^^^^
Rusty old river, rusty old boat.
$& ^^^^^
Rusty old river, rusty old boat.
$& ^^^^^
rred>
And that's about it...
This examples demonstrates a regular expression for stripping C comments from source code.
First, we set up a test string, after changing the quote character to avoid needing backslashes for the quotes in our test string:
hr@moonshine:~$ rred
rred regular expression debugger version 0.1
Commands:
t<text> Set text to match against
e<regexp> Set regular expression
m<text> Set match modifiers (default is 'g')
q<char> Set text quotes (default is '"')
d<char> Set expression delimiters (default is '/')
x, ^D exit
rred> q'
$text = ''; $text =~ m//g;
rred> tfoo("I am not a comment"); /* "but I am" */
$text = 'foo("I am not a comment"); /* "but I am" */'; $text =~ m//g;
rred>
Next, set up an expression to find the comments. Simple, right? Again we change the delimiters so we don't need to escape the '/' characters.
rred> d#
$text = 'foo("I am not a comment"); /* "but I am" */'; $text =~ m##g;
rred> e/\*.*?\*/
$text = 'foo("I am not a comment"); /* "but I am" */'; $text =~ m#/\*.*?\*/#g;
foo("I am not a comment"); /* "but I am" */
$& ^^^^^^^^^^^^^^^^
rred>
Hey, it works. Except...
rred> tfoo("I am not a /* comment */"); /* "but I am" */
$text = 'foo("I am not a /* comment */"); /* "but I am" */'; $text =~ m#/\*.*?\*/#g;
foo("I am not a /* comment */"); /* "but I am" */
$& ^^^^^^^^^^^^^
foo("I am not a /* comment */"); /* "but I am" */
$& ^^^^^^^^^^^^^^^^
rred>
That's not right. Better try something a little more complicated (taken from perlfaq6):
rred> e/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)
$text = 'foo("I am not a /* comment */"); /* "but I am" */ '; $text =~ m#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#g;
foo("I am not a /* comment */"); /* "but I am" */
$& ^^^^
$2 ^^^^
foo("I am not a /* comment */"); /* "but I am" */
$& ^^^^^^^^^^^^^^^^^^^^^^^^^^
$2 ^^^^^^^^^^^^^^^^^^^^^^^^^^
$3 ^
foo("I am not a /* comment */"); /* "but I am" */
$& ^^^
$2 ^^^
foo("I am not a /* comment */"); /* "but I am" */
$& ^^^^^^^^^^^^^^^^
rred>
Success! The new expression accumulates all non-comment parts of the string in $2, so we can use it in a substitution to get rid of the comments.