简体   繁体   中英

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. 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:

Match #0 Length: 16 Range: 1-16

ipsum lorem 30/9

Group #1 Length: 12

ipsum lorem

Group #2 Length: 2

30

Group #3 Length: 1

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]+) )

Try: [a-zA-Z\\s]*([\\d]*)\\/?([\\d]*)? . That will get the first part in the first group and anything after the / in the second group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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