Whats wrong with this regex -- parsing city province???

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Whats wrong with this regex -- parsing city province???

Post by alex.barylski »

Code: Select all

preg_split('#(\W+)\([A-Z]{2}\)#', $subject);
Basically I am passing it input in the form:

Code: Select all

Winnipeg (MB)
Regina (SK)
Toronto (ON)
You see the pattern I'm sure...I want two parameters back, first being the city (with possible white space), the second being the province abbreviation wrapped in round brakctes with a single space between them and the city name.

Obviously the regex I have written does not work, it only matches the city and ignores the province...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Whats wrong with this regex -- parsing city province???

Post by pickle »

Why are you using preg_split()? preg_match() would seem to be a better fit.

\W won't work well - what about "Trois Rivieres" or "Flin Flon", only the first word would be found.

Use this:
/(.*?) \((.*)\)/
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Whats wrong with this regex -- parsing city province???

Post by prometheuzz »

pickle wrote:Why are you using preg_split()? preg_match() would seem to be a better fit.
...
Possibly, unless he wants to get the two parts (city & province) back separated, then split is appropriate here.

@PCSpectra, you could split on the white space that has a '(' in front of it:

Code: Select all

print_r(preg_split('/\s(?=\()/', 'Trois Rivieres (AB)'));
/*
Array
(
    [0] => Trois Rivieres
    [1] => (AB)
)
*/
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Whats wrong with this regex -- parsing city province???

Post by alex.barylski »

Thanks to both of you, I have tried the latter example:

Code: Select all

$search_region = preg_split('#/\s(?=\()/#', $request->search_region, null, PREG_SPLIT_DELIM_CAPTURE);
Still getting the result:

Code: Select all

Array
(
    [0] => Winnipeg (MB)
)
Any ideas? Not all city names will have spaces, but some will most definetely.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Whats wrong with this regex -- parsing city province???

Post by prometheuzz »

It works fine:

Code: Select all

print_r(preg_split('/\s(?=\()/', 'Winnipeg (MB)'));
/*
Array
(
    [0] => Winnipeg
    [1] => (MB)
)
*/
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Whats wrong with this regex -- parsing city province???

Post by alex.barylski »

I copied your code verbatim and that seems to have fixed the issue, thanks a bunch :)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Whats wrong with this regex -- parsing city province???

Post by prometheuzz »

PCSpectra wrote:I copied your code verbatim and that seems to have fixed the issue, ...
But you see the difference, right? You're using double delimiters (both '/' and '#') while only one is needed.
PCSpectra wrote:... thanks a bunch :)
You're welcome.
Post Reply