image
image

Go Back   macosx.com > Mac Help Forums > Mac OS X System & Mac Software

Reply
 
Thread Tools
  #1  
Old November 8th, 2001, 07:37 PM
Registered User
 
Join Date: Oct 2001
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
wzpgsr is on a distinguished road
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";
}
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!
Reply With Quote
  #2  
Old November 8th, 2001, 11:18 PM
blb blb is offline
`'
 
Join Date: Apr 2001
Location: CO, USA
Posts: 651
Thanks: 0
Thanked 0 Times in 0 Posts
blb is on a distinguished road
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\])/) {
Of course, since you know that first should be the '[' character, you probably don't need to save it...personally, however, I would write this as:

Code:
if( $tag =~ /\[([a-z]+)\]([^[]*)\[\/\1\]/) {
since the only info you really need is the tag name and data in the tag. Also, I changed the data match to be [^[]* as that will match anything which isn't a '[' instead of the less-efficient .+? (which causes the parser to backtrack until it finds the match following .+?; a great regex book is Friedl's Mastering Regular Expressions).
Reply With Quote
  #3  
Old November 9th, 2001, 12:46 AM
Registered User
 
Join Date: Oct 2001
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
wzpgsr is on a distinguished road
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.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

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


All times are GMT -5. The time now is 01:21 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.