简体   繁体   English

正则表达式PHP,在特定位置查找字符

[英]Regex PHP, Find characters in specific position

I explain my problem : I'm working on different kind of address 我解释了我的问题:我正在使用其他地址

" 25 Down Street 15000 London "

" 25 B Down Street 15000 London "

" Building A 25 Down Street 15000 London "

I found a way to determine which is the number of the street on all case with this regex : 我找到了一种使用此正则表达式确定所有情况下的街道编号的方法:

 `^([1-9][0-9]{0,2}(?:\s*[A-Z])?)\b`

But now i got a problem that i can't solve, i need when the case is real to determine characters which are before the street's number . 但是现在我遇到了一个我无法解决的问题,我需要在案子是真的情况下才能确定这条街号之前的字符。

Example : " Building 2 25 Down Street 15000 London " i need here to find only "Building 2" 示例:“ Downtown 1 25 号楼2号楼 15000伦敦”,我在这里只需要查找“ 2号楼”

I understand that i have to find characters before the first number of this string. 我了解我必须在此字符串的第一个数字之前找到字符。

Keep searching on my own but will be great if someone got a solution for me . 继续自己搜索,但是如果有人为我找到解决方案,那将是很棒的。

Thank you . 谢谢 。

Edit my code now is : 现在编辑我的代码是:

preg_match('/^(.*?)\d+\s+\D+/', $cleanAdressNode, $result, PREG_OFFSET_CAPTURE,0);
        print $result[0][0];

        return $result[0][0];

and the result now is : Résidence Les Thermes 1 15 boulevard Jean Jaurès instead of only : Résidence Les Thermes 1 现在的结果是:RésidenceLes Thermes 1 15大道JeanJaurès而不是仅: RésidenceLes Thermes 1

How about: 怎么样:

preg_match('/^(\D*)/', $str, $match);

You will find in $match[1] everything that is not a digit at the begining of the string. 您将在$match[1]找到字符串开头不是数字的所有内容。

According to your example: 根据您的示例:

preg_match('/^(.*?)\d+\s+\D+/', $str, $match);

If you only want to match the first non-numeric characters, ^([^0-9]*) should do the trick. 如果只想匹配前几个非数字字符,则可以使用^([^0-9]*) It uses class negation to grab every non-numeric characters at the start of the string. 它使用类求反来获取字符串开头的所有非数字字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM