Regex for splitting a german address into its parts

前端 未结 6 747
孤街浪徒
孤街浪徒 2021-02-09 18:09

Good evening,

I\'m trying to splitting the parts of a german address string into its parts via Java. Does anyone know a regex or a library to do this? To split it like t

6条回答
  •  别那么骄傲
    2021-02-09 18:33

    public static void main(String[] args) {
        String data = "Name der Strase 25a 88489 Teststadt";
        String regexp = "([ a-zA-z]+) ([\\w]+) (\\d+) ([a-zA-Z]+)";
    
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher = pattern.matcher(data);
        boolean matchFound = matcher.find();
    
        if (matchFound) {
            // Get all groups for this match
            for (int i=0; i<=matcher.groupCount(); i++) {
                String groupStr = matcher.group(i);
                System.out.println(groupStr);
            }
        }System.out.println("nothing found");
                    }
    

    I guess it doesn't work with german umlauts but you can fix this on your own. Anyway it's a good startup.

    I recommend to visit this it's a great site about regular expressions. Good luck!

提交回复
热议问题