On Apr 7, 2005, at 1:26 PM, Mats Persson wrote:
OK, for way too many hours I have been trying to get this seemingly simple Perl compatible regex to work out, but I just can't seem to do it. I'm about to give up, move to a dark cave and shun computers for life. :(
The problem:
I have a string that is looking something like this: /a/b/c/d/ the string can be just /a/ or it can be /a/b/..../z/ very long.
I would like to catch all the various bits in this string [ /a/b/c/d/ ] as follows: $1 = /a/ $2 = b/ $3 = c/ $4 = d/
and so on for each added bit. The bits in between the "/" contains mainly [alphnums].
I've tried every regex version of this that I can think of and most don't return a damn thing, and others return the wrong things. A few days ago I thought I got this regex stuff, but now I'm in serious doubt.
I don't think you can do this, as I mentioned on IRC. You can easily match an entire string of that form: /([^/]*/)+
The problem comes in getting the captures you want. Each set of parens will get you *one* capture, not many. As TextMate's help puts it: "On repetition, the last string captured is reported."
If you want to get multiple captures, you have to have multiple parens, which necessitates knowing how many (or at least the max number of) captures you have. For example:
/([^/]*/)([^/]*/)?([^/]*/)?([^/]*/)?([^/]*/)?([^/]*/)?([^/]*/)? ([^/]*/)*
Typically you'd solve this by having a regex that matches one time, and reporting multiple matches. In PHP, this would mean preg_match_all(), though you could solve this problem easier with explode().