I am learning Perl regular expressions (on an OS X 10.1 workstation with the default Perl install).
I am trying to duplicate the functionality of many of these message boards that allow pseudo-HTML code, for example using [*b*]text here[/*b*] to bold a phrase (minus the asterisks). As such, I have written the following code to test the regular expression (bear with me):
When run as is, this code returns "no match!", but as far as I can tell it should match. When I remove the parentheses from the initial atom:
it matches properly, which seems strange because as far as I know the parentheses should have no effect on the match inside; they just store the value into a variable for later use. Unfortunately, I want to grab this first atom using $1 so I need the parantheses there.
I'd appreciate if someone with regexp know-how would enlighten me. I'm going batty!
I am trying to duplicate the functionality of many of these message boards that allow pseudo-HTML code, for example using [*b*]text here[/*b*] to bold a phrase (minus the asterisks). As such, I have written the following code to test the regular expression (bear with me):
Code:
$tag = "[bold]This is some text![/bold]";
if ($tag =~ /(\[)([a-z]+)(\])(.+?)(\[\/\1\])/) {
print "matched!\n";
print "atom 1 = $1\n";
print "atom 2 = $2\n";
print "atom 3 = $3\n";
print "atom 4 = $4\n";
print "atom 5 = $5\n";
print "atom 6 = $6\n";
}
else {
print "no match!\n";
}
When run as is, this code returns "no match!", but as far as I can tell it should match. When I remove the parentheses from the initial atom:
Code:
if ($tag =~ /\[([a-z]+)(\])(.+?)(\[\/\1\])/) {
it matches properly, which seems strange because as far as I know the parentheses should have no effect on the match inside; they just store the value into a variable for later use. Unfortunately, I want to grab this first atom using $1 so I need the parantheses there.
I'd appreciate if someone with regexp know-how would enlighten me. I'm going batty!