fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. // String to be scanned to find the pattern.
  11. String line = "Bygholm Søpark 21B";
  12. String pattern = "\\A\\s*\r\n" +
  13. "(?: #########################################################################\r\n" +
  14. " # Option A: [<Addition to address 1>] <House number> <Street name> #\r\n" +
  15. " # [<Addition to address 2>] #\r\n" +
  16. " #########################################################################\r\n" +
  17. " (?:(?<AAdditiontoaddress1>.*?),\\s*)? # Addition to address 1\r\n" +
  18. "(?:No\\.\\s*)?\r\n" +
  19. " (?<AHousenumber1>\\pN+[a-zA-Z]?(?:\\s*[-/\\pP]\\s*\\pN+[a-zA-Z]?)*) # House number\r\n" +
  20. "\\s*,?\\s*\r\n" +
  21. " (?<AStreetname1>(?:[a-zA-Z]\\s*|\\pN\\pL{2,}\\s\\pL)\\S[^,\\#]*?(?<!\\s)) # Street name\r\n" +
  22. "\\s*(?:(?:[,/]|(?=\\#))\\s*(?!\\s*No\\.)\r\n" +
  23. " (?<AAdditiontoaddress2>(?!\\s).*?))? # Addition to address 2\r\n" +
  24. "| #########################################################################\r\n" +
  25. " # Option B: [<Addition to address 1>] <Street name> <House number> #\r\n" +
  26. " # [<Addition to address 2>] #\r\n" +
  27. " #########################################################################\r\n" +
  28. " (?:(?<BAdditiontoaddress1>.*?),\\s*(?=.*[,/]))? # Addition to address 1\r\n" +
  29. " (?!\\s*No\\.)(?<BStreetname>\\S\\s*\\S(?:[^,\\#](?!\\b\\pN+\\s))*?(?<!\\s)) # Street name\r\n" +
  30. "\\s*[/,]?\\s*(?:\\sNo\\.)?\\s+\r\n" +
  31. " (?<BHousenumber>\\pN+\\s*-?[a-zA-Z]?(?:\\s*[-/\\pP]?\\s*\\pN+(?:\\s*[-a-zA-Z])?)*|[IVXLCDM]+(?!.*\\b\\pN+\\b))(?<!\\s) # House number\r\n" +
  32. "\\s*(?:(?:[,/]|(?=\\#)|\\s)\\s*(?!\\s*No\\.)\\s*\r\n" +
  33. " (?<BAdditiontoaddress2>(?!\\s).*?))? # Addition to address 2\r\n" +
  34. ")\r\n" +
  35. "\\s*\\Z";
  36.  
  37. // Create a Pattern object
  38. Pattern r = Pattern.compile(pattern, Pattern.COMMENTS);
  39.  
  40. // Now create a matcher object.
  41. Matcher m = r.matcher(line);
  42. if (m.find()) {
  43. System.out.println("B_Street_name: " + m.group("BStreetname") );
  44. System.out.println("B_House_number: " + m.group("BHousenumber") );
  45. System.out.println("B_Addition_to_address_2: " + m.group("BAdditiontoaddress2") );
  46. }else {
  47. System.out.println("NO MATCH");
  48. }
  49.  
  50. }
  51. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
B_Street_name: Bygholm Søpark
B_House_number: 21B
B_Addition_to_address_2: null