[英]Address Parsing PHP Regex
I got struct with to parse the Door number/Flat number from address.我得到了 struct with 来解析地址中的门号/公寓号。 So kindly guide me how to do this using regex.
所以请指导我如何使用正则表达式来做到这一点。 Also i have tried with following regex to parse that but it doesn't works what we expected.
我也试过用下面的正则表达式来解析它,但它不符合我们的预期。
preg_match('![0-9/-]+!', $address, $matches);
Also i have added few sample inputs and expected output我还添加了一些样本输入和预期输出
Input
#302 MEENA RESIDENCY NEW ALLAPUR 600032
Expected Output
302
Input
No 35/2 2nd main 2nd cross subramanium l ayout viginapura ramurthy nagar 600032
Expected Output
35/2
Input
17-13-54 jonnaguddi vzm,near bhashyam school 600032
Expected Output
17-13-54
Input
Floor 4,plot no 285 2 birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
285
Input
3-a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3-a
Input
3a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3a
Input
3/a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3/a
Input
3/2a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3/2a
Input
3a/2 birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3a/2
Input
3/2-a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3/2-a
Input
3-a/2 birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3-a/2
Thank you谢谢
You may use您可以使用
~(?:#|\bno\s*|^)(\d+(?:[/-]?\w+)*)~im
or或者
~(?:#|\bno\s*|^)\K\d+(?:[/-]?\w+)*~im
See the regex demo查看正则表达式演示
Details :详情:
(?:#|\\bno\\s*|^)
- either of: (?:#|\\bno\\s*|^)
- 任一:
#
- a hash sign #
- 一个哈希符号\\bno\\s*
- a whole word followed with 0+ whitespaces \\bno\\s*
- 整个单词后跟 0+ 个空格^
- start of a line ^
- 一行的开始\\K
- a match reset operator discarding the text matched so far in the current iteration \\K
- 匹配重置运算符丢弃当前迭代中到目前为止匹配的文本\\d+
- 1+ digits \\d+
- 1+ 位数(?:[\\/-]?\\w+)*
- 0+ sequences of: (?:[\\/-]?\\w+)*
- 0+ 序列:
[\\/-]?
- an optional /
or -
/
或-
\\w+
- 1+ letters/digits/ _
\\w+
- 1+ 个字母/数字/ _
This regex selects the desired numbers in group 1:此正则表达式在第 1 组中选择所需的数字:
/^(?:#|.*?\bno\s+)?(\d[\w\-\/]*)/gmi
It performs the same selection as Wiktor Stribizew's one, but in less steps.它执行与 Wiktor Striizew 相同的选择,但步骤更少。
^
asserts the start of a line ^
断言一行的开始(?:#|.*?\\bno\\s+)?
indicates the number is possibly preceded by:#
character #
字符no
followed by at least one spaceno
后跟至少一个空格(\\d[\\w\\-\\/]*)
captures the number that start with a number and can be composed of a letter, a number, a dash -
or a slash /
. (\\d[\\w\\-\\/]*)
捕获以数字开头和可以由字母,数字,破折号的数目-
或斜线/
。If you test your string one at a time, the
m
flag (multiline) isn't necessary.如果一次测试一个字符串,则不需要
m
标志(多行)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.