简体   繁体   English

用正则表达式解析地址

[英]Parsing address with Regex

I want to be able to parse a String into several Strings of information using regular expressions. 我希望能够使用正则表达式将一个字符串解析为多个信息字符串。 Let's for instance use an address: "My Street 10 90210 Beverly Hills". 例如,使用一个地址:“我的街10号90210比佛利山庄”。 An example of Strings I could create from this: 我可以从中创建一个字符串示例:

  • My Street – street name My Street -街道名称
  • 10 – house number 10 –门牌号码
  • 90210 – zip code 90210 –邮政编码
  • Beverly Hills – city Beverly Hills –城市

I've been trying to create a pattern for it use, but I'm a little lost when it comes to analyze the String and dissect it. 我一直在尝试创建一种模式以供使用,但是在分析字符串并进行剖析时,我有点迷失了。

Can someone get me started on this? 有人可以让我开始吗?

I would use string.split function. 我会使用string.split函数。

String s = "My Street 10 90210 Beverly Hills";
String parts[] = s.split("\\s+(?=\\d+\\s+\\d+)|(?<=\\d+)\\s+(?=[A-Z])|(?<=\\d+)\\s+(?=\\d+)");
System.out.println(Arrays.toString(parts));

Output: 输出:

[My Street, 10, 90210, Beverly Hills]

Explanation: 说明:

  • \\\\s+(?=\\\\d+\\\\s+\\\\d+) Matches one or more spaces only if it's followed by one or more digits plus one or more spaces plus one or more digits. \\\\s+(?=\\\\d+\\\\s+\\\\d+)仅匹配一个或多个空格,后跟一个或多个数字加一个或多个空格加一个或多个数字。 So that space before house number would satisfy this condition . 因此,门牌号之前的空间将满足此条件。 So it got matched. 所以它匹配了。

  • | Called alternation operator. 称为交替运算符。

  • (?<=\\\\d+)\\\\s+(?=[AZ]) Matches one or more spaces which are preceded by one or more digits and then followed by a capital letter. (?<=\\\\d+)\\\\s+(?=[AZ])匹配一个或多个空格,这些空格之前是一个或多个数字,然后是大写字母。 So the spaces before the string city would satisfy this condition and got matched. 因此,字符串city之前的空间将满足此条件并得到匹配。

  • (?<=\\\\d+)\\\\s+(?=\\\\d+) This matches all the spaces which are in-between the digits. (?<=\\\\d+)\\\\s+(?=\\\\d+)匹配数字之间的所有空格。 So the spaces between house-number and zip-code got matched. 因此,门牌号和邮政编码之间的空格是匹配的。

  • Splitting your input according to the matched spaces will give you the desired output. 根据匹配的空间拆分输入,将为您提供所需的输出。

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

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