-2

Edit again to trying to make it more clear.

Wich php regex pattern will give me a match array containing always 2 values wich is the 2 part of a string splitted by the "wordA" or "wordB". If the string do not containt those word, simply return the string as the first array an null in the second array.

Exemple:

preg_match("pattern","foo wordA bar",$match), $match will contain array['foo', 'bar']
preg_match("pattern","foo wordB bar",$match), $match will contain array['foo', 'bar']
preg_match("pattern","foo bar test",$match), $match will contain array['foo bar test', null]

I know that $match first value is always the string so I just don't write it.

OLD question:

I need to split a one line address into part. I can't find a way to capture street part but dont include the APP or APT word if present and if present, capture the 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 I have so far is: /^(\d+)(.*), (.*)(?:APP|APT)(?:\s*(.*))?$/i wich wook with exemple 1 and 2. If I try to make the alternative (APP|APT) optionnal by adding an ? after it, then the third match include the word APP or APT...

Any idea how to exclude the optionnal and alternative APP or APT word from match?

Thank you

EDIT:

I can simplify the problem: How can I regex a string so the match return the same string minus the word APP or APT if he is present in the middle of it.

4

3 回答 3

0

对于“简单”版本

 var_dump(preg_replace ( "/ apt|app /i" , "" ,"5847A, rue Prince Arthur APT 22"  ));

覆盖它

输出

5847A, rue Prince Arthur 22

更难的版本你需要更多的上下文,比如为什么逗号看起来像它们一样。

硬版

([0-9]*)([a-z]?),(((?!app|apt).)*)(?:app|apt)?(.*)

似乎适用于您的测试用例

于 2013-07-11T20:34:59.463 回答
0

正如@MadaraUchiha 指出的那样,在地址上运行正则表达式是个坏主意,因为它们可以是任何格式。

如果你知道你有一致的地址,那么我想你可以使用正则表达式:

^([0-9]+)([A-Z])?,\s(?:(.*?)\s(?:APP|APT)\s(.*)|(.*))$

和替换:

$1,$2,$3$5,$4

是它的表现。

它与您的非常相似(我改变了一些东西)并添加了一个 or ( ) 运算符来处理没有or|的第二种类型的地址。APPAPT

如果您想要一致的匹配数量,也许是这个?

^([0-9]*)([A-Z]?),((?:(?!\sAPP|\sAPT).)*)(?:\sAPP|\sAPT)?(.*)$

正则表达式 101 示例

于 2013-07-11T20:35:12.173 回答
0

我认为这应该有效:

$pattern = "/\bAPP|APT\b/i";
$subject = "1111, Sherwood street";
echo preg_replace($pattern, "", $subject);
于 2013-07-11T21:19:26.490 回答