edit again trying make more clear.
wich php regex pattern give me match array containing 2 values wich 2 part of string splitted "worda" or "wordb". if string not containt word, return string first array null in second array.
exemple:
preg_match("pattern","foo worda bar",$match), $match contain array['foo', 'bar'] preg_match("pattern","foo wordb bar",$match), $match contain array['foo', 'bar'] preg_match("pattern","foo bar test",$match), $match contain array['foo bar test', null]
i know $match first value string don't write it.
old question:
i need split 1 line address part. can't find way capture street part dont include app or apt word if present , if present, capture words after it.
for exemple:
"5847a, rue principal app a" should match: (5847, a, rue principal,a)
"5847a, rue prince arthur apt 22" should match: (5847, a, rue prince arthur, 22)
"1111, sherwood street" should match: (1111, , sherwood street, )
i'm using php.
what have far is: /^(\d+)(.*), (.*)(?:app|apt)(?:\s*(.*))?$/i
wich wook exemple 1 , 2. if try make alternative (app|apt) optionnal adding ? after it, third match include word app or apt...
any idea how exclude optionnal , alternative app or apt word match?
thank you
edit:
i can simplify problem: how can regex string match return same string minus word app or apt if present in middle of it.
as @madarauchiha pointed out, it's bad idea run regex on address since can in format.
if know have consistent addresses, guess can use regex:
^([0-9]+)([a-z])?,\s(?:(.*?)\s(?:app|apt)\s(.*)|(.*))$
and replace:
$1,$2,$3$5,$4
here's how it's performing.
it's pretty similar yours (i changed few things) , added or (|
) operator address second type of addresses without app
or apt
.
if want consistent number of matches, maybe this?
^([0-9]*)([a-z]?),((?:(?!\sapp|\sapt).)*)(?:\sapp|\sapt)?(.*)$
Comments
Post a Comment