|
#1
| |||
| |||
| Regular Expression Gurus Out There? 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): 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";
} Code: if ($tag =~ /\[([a-z]+)(\])(.+?)(\[\/\1\])/) { I'd appreciate if someone with regexp know-how would enlighten me. I'm going batty! |
|
#2
| |||
| |||
| Well, the immediate problem is one of those Doh! type problems, namely, you want the last piece to match \2 not \1 since \1 is a '[': Code: if ($tag =~ /(\[)([a-z]+)(\])(.+?)(\[\/\2\])/) { Code: if( $tag =~ /\[([a-z]+)\]([^[]*)\[\/\1\]/) { |
|
#3
| |||
| |||
| That really *is* a doh! I started out with something very similar to what you propose, but forgot to update the variable somewhere along the line. Thanks for the additional insight...I am going to check out that book. |
![]() |
| Thread Tools | |
|
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| java regular expressions | cfleck | Software Programming & Web Scripting | 0 | September 16th, 2003 10:42 PM |
| regular expression | shadowfax | Unix & X11 | 3 | July 22nd, 2003 04:48 AM |
| Admin vs. regular user? | anilsen | Mac OS X System & Mac Software | 4 | April 2nd, 2003 05:36 PM |
| Airport Extreme and regular Airport? | martinatkinson | Hardware & Peripherals | 2 | March 2nd, 2003 04:41 PM |
| regular users cannot set date and time | solrac | Mac OS X System & Mac Software | 0 | August 12th, 2002 04:02 AM |