简体   繁体   English

正则表达式解析住所和公寓的地址

[英]Regex to parse address house and apartment numbers

currently I'm using this regex 目前我正在使用此正则表达式

private string DigExp = @"[^\d]";

that way: 那样:

Regex.Match(Address, DigExp, "")

while string address usually containes characters and numbers.. let's say if address is "ipsum lorem 30/9" or "ipsum lorem 309" i still get 309 as result in both examples. 而字符串地址通常包含字符和数字。.让我们说一下,如果地址是“ ipsum lorem 30/9”或“ ipsum lorem 309”,则在两个示例中我仍然得到309。 i need two regex to solve it, one to match first number until the / if exist at all, and another one to match the second that should be after / at the end of the string and may not exist at all. 我需要两个正则表达式来解决它,一个正则表达式匹配第一个数字直到/如果存在(如果存在),另一个正则表达式匹配第二个正则表达式,该第二个表达式应该在/后面并在字符串末尾并且可能根本不存在。

I need address numbers seperated into regex groups withous the / 我需要使用/分隔成正则表达式组的地址号码

can you please guide me to achieve my goal? 您能指导我实现目标吗? thanks. 谢谢。

您可以轻松将它们分为两组:

(\d+)/?(\d*)

尝试使用以下模式:

([\d\/\d]+|\d+)  

By using groups, you can use the following regex to isolate the different parts of the address string. 通过使用组,可以使用以下正则表达式隔离地址字符串的不同部分。 The following separates the address into the all of the words, and the numbers before and after the optional slash: 下面将地址分为所有单词,以及可选斜杠之前和之后的数字:

((?:[a-zA-Z]+\s)*)([\d]+)(?:(?:\/)(\d+))?

eg: "ipsum lorem 30/9" becomes: 例如: "ipsum lorem 30/9"变为:

Match #0 Length: 16 Range: 1-16 匹配#0长度:16范围:1-16

ipsum lorem 30/9 ipsum lorem 30/9

Group #1 Length: 12 组#1长度:12

ipsum lorem ipsum lorem

Group #2 Length: 2 组#2长度:2

30 30

Group #3 Length: 1 组#3长度:1

9 9

Note: The follow regex adds the ability to match words after the numbers in a fourth group: 注意:遵循正则表达式可以在第四组数字之后匹配单词:

((?:[a-zA-Z]+\\s) )([\\d]+)(?:(?:/)(\\d+))?((?:\\s[a-zA-Z]+) ) ((?? [a-zA-Z] + \\ s) )([\\ d] +)(?:(?:/)(\\ d +))?((?:\\ s [a-zA-Z] +)

Try: [a-zA-Z\\s]*([\\d]*)\\/?([\\d]*)? 试试: [a-zA-Z\\s]*([\\d]*)\\/?([\\d]*)? . That will get the first part in the first group and anything after the / in the second group. 这将在第一组中获得第一部分,而在第二组中将在/之后获得任何东西。

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

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