tags:

views:

190

answers:

3

This is more of a generic regex question than a PHP-specific one.

I am given different strings that may look like:

A/B/PA ID U/C/D

And I'm trying to extract the segment in the middle slashes that has spaces ("/PA ID U") using:

preg_match('/(\/PA .+)(\/.+|$)/', '', $matches);

However, instead of getting "/PA ID U" as I was expecting, I was getting "/PA ID U/C/D".

How can I make it prioritize matching "/.+" over "$" in that last group?


Additional notes:

I need that last group to match either another "/somethingsomthing" or "" because the string varies a lot. If I only match for the "/.+", I won't be able to get the "/PA ID U" if it's at the end of the line, such as in "A/B/PA ID U".

Basically, I need to be able to extract specific segments like so:

Given: "A/B/PA ID U/PA ID U/C/D"

Extract: (A), (B), (PA ID U), (PA ID U), (C), (D)


[UPDATE]

I'm trying to avoid using split() or explode() because that would mean that I have to match the "PA ID U" pattern separately. Aside from merely extracting the slash-separated segments, I need to validate that the substrings match specific patterns.

+2  A: 

I think you can most effectively use split to accomplish what you want.

split('/',$string);

See: php manual

DasBoot
A: 

necramirez,

(\w+\s?)+

should work

Lieven
+1  A: 

Your regular expression is not working because the .+ is being greedy. You could fix it by adding a non-greedy modifier (a ?) to your first .+ as such:

preg_match('/(\/PA .+?)(\/.+|$)/', '', $matches);

You could alternatively do:

'/\/(PA [^\/]+)(\/.+|$)/'

I moved the slash outside of the parens to avoid capturing that (I presume you're not interested in the slash). The [^\/]+ will capture any character up to the next slash.

wuputah
Exactly what I needed! Thanks! :DI also found this exact solution in http://www.regular-expressions.info/repeat.html
Nikki Erwin Ramirez
I'm a bit at a loss here. I can't get this regex to extract the Given "A/B/PA ID U/PA ID U/C/D" into (A), (B), (PA ID U), (PA ID U), (C), (D). What regex flavor does php use? Perl? Pythong? Any other?
Lieven
preg_match is implemented as a call to libpcre. PCRE = Perl Compatible Regular Expressions
wuputah