将字符串分割成3个部分(文本 – 数字 – 其他字符),如果可能的话。

huangapple go评论102阅读模式
英文:

Split string in 3 blocks (Text - Number - Other characters) if possible

问题

  1. List<String> AllAddress = Arrays.asList("Karbwqeaf 11D", "Jablunkovska 21/2", "Tastoor Nstraat 43", "Schzelkjedow", "Heajsd 3/5/7 m 344", "Lasdasdt seavees 3., 729. tasdasd F 2.", "ul. Pasydufasdfa 73k/120");
  2. for (String Address : AllAddress) {
  3. String block1 = "";
  4. String block2 = "";
  5. String block3 = "";
  6. Pattern pattern = Pattern.compile("([A-Za-z]+)\\s(\\d+)(.*)");
  7. Matcher matcher = pattern.matcher(Address);
  8. if (matcher.matches()) {
  9. block1 = matcher.group(1);
  10. block2 = matcher.group(2);
  11. block3 = matcher.group(3);
  12. System.out.println("Block1: " + block1);
  13. System.out.println("Block2: " + block2);
  14. System.out.println("Block3: " + block3);
  15. }
  16. }
英文:

I need to divide a string into 3 blocks at most

  • The first block is only for letters
  • The second only numbers
  • The third the remainder

Examples:

  1. Karbwqeaf 11D
  2. Jablunkovska 21/2
  3. Tastoor Nstraat 43
  4. Schzelkjedow
  5. Heajsd 3/5/7 m 344
  6. Lasdasdt seavees 3., 729. tasdasd F 2.
  7. ul. Pasydufasdfa 73k/120

I need to split like this:

  1. Block1: Karbwqeaf
  2. Block2: 11
  3. Block3: D
  4. Block1: Jablunkovska
  5. Block2: 21
  6. Block3: /2
  7. Block1: Tastoor Nstraat
  8. Block2: 43
  9. Block3:
  10. Block1: Schzelkjedow
  11. Block2:
  12. Block3:
  13. Block1: Heajsd
  14. Block2: 3
  15. Block3: /5/7 m 344
  16. Block1: Lasdasdt seavees 3
  17. Block2: 3
  18. Block3: ., 729. tasdasd F 2.
  19. Block1: ul. Pasydufasdfa
  20. Block2: 73
  21. Block3: k/120

Below my code, but I don't know how to do it so that all my requirements are met. Any idea?

  1. List&lt;String&gt; AllAddress = Arrays.asList(&quot;Karbwqeaf 11D&quot;, &quot;Jablunkovska 21/2&quot;, &quot;Tastoor Nstraat 43&quot;, &quot;Schzelkjedow&quot;, &quot;Heajsd 3/5/7 m 344&quot;, &quot;Lasdasdt seavees 3., 729. tasdasd F 2.&quot;, &quot;ul. Pasydufasdfa 73k/120&quot;);
  2. for (String Address : AllAddress) {
  3. String block1 = &quot;&quot;;
  4. String block2 = &quot;&quot;;
  5. String block3 = &quot;&quot;;
  6. Pattern pattern = Pattern.compile(&quot;(.+)\\s(\\d)(.*)&quot;);
  7. Matcher matcher = pattern.matcher(Address);
  8. if(matcher.matches()) {
  9. block1 = matcher.group(1);
  10. block2 = matcher.group(2);
  11. block3 = matcher.group(3);
  12. System.out.println(&quot;block1 = &quot; + block1);
  13. System.out.println(&quot;block2 = &quot; + block2);
  14. System.out.println(&quot;block3 = &quot; + block3);
  15. }
  16. }

答案1

得分: 2

你可以使用3个捕获组,其中第二个组匹配1个或多个数字,第三个组匹配任意字符0次或多次。

  1. ^([^\d\r\n]+)(?:\h+(\d+)(.*))?$

解释

  • ^ 字符串的开始
  • ( 捕获组 1
    • [^\d\r\n]+ 匹配除换行符或数字之外的任意字符
  • ) 关闭 捕获组 1
  • (?: 非捕获组
    • \h+ 匹配1个或多个水平空白字符
    • (\d+)(.*)捕获组 2中捕获1个或多个数字,在捕获组 3中捕获0个或多个任意字符
  • )? 关闭非捕获组,并使其变为可选
  • $ 字符串的结尾

正则表达式演示链接

英文:

You can use 3 capturing groups, where the second group matches 1 or more digits and the 3rd group matches any character 0+ times.

  1. ^([^\d\r\n]+)(?:\h+(\d+)(.*))?$

Explanation

  • ^ Start of string
  • ( Capture group 1
    • [^\d\r\n]+ Match any char except a newline or digit
  • ) Close group 1
  • (?: Non capture group
    • \h+ Match 1+ horizontal whitespace chars
    • (\d+)(.*) Capture 1 or more digits in group 2 and capture 0 or more times any character in group 3
  • )? Close the non capture group and make it optional
  • $ End of string

Regex demo

huangapple
  • 本文由 发表于 2020年9月23日 20:11:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64027691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定