[TxMt] OT: Perl Compatible Regex problem I can't solve

Mats Persson mats at imediatec.co.uk
Mon Apr 11 09:47:41 UTC 2005


Thanks everyone for your replies. Much appreciated!

Even though I've been working with regex on and off for many years I 
still find myself communicating with it in gestures and facial 
expressions rather than fluent talk. ;-)

Yes, I am very well aware of the various split on "/", explode() in PHP 
and so on implementations, but they are all slightly imperfect 
workarounds that I - falsely it turns out - assumed could be cut out 
with a single code line of clever regex.  Hah! how stupid one can be! 
or is it the tools I'm working with ? ;-)

The strings I'm working with follows this general format:
/a/b/c/d/	 =>  (/a/)(b/)(c/)(d/)	-->  captures all of them
/a/b/c/d	=>	(/a/)(b/)(c/)		--> ignore the last bit [ d ] as it does not 
have an ending slash

So the regex I would have wanted was something like this (simplified in 
my logic):
(/\w+/)(\w+/)*?    returning an array like this ( 1 => "/a/",  2 => 
"b/", 3 => "c/" ) (skipping /d )

I got most of the above working through PHP's preg_match_all() albeit 
slightly clumsily.

However, I realised - which is far too often the case when I'm 
encountering any problems - that my initial approach was wrong, and 
worked around the whole issue, so what I was trying to do is now done 
in less code just as I wanted in the first place. :)

The quote of the month was provided by Don Kalar on 7 Apr 2005, at 
20:46:
> "Some people, when confronted with a problem, think "I know, I'll use 
> regular expressions." Now they have two problems."
> --Jamie Zawinski, in comp.lang.emacs


Kind regards,

Mats

----
"TextMate, coding with an incredible sense of joy and ease"
- www.macromates.com -




More information about the textmate mailing list