Regular Expression to find “lastname, firstname middlename” format

后端 未结 5 1733
挽巷
挽巷 2021-01-18 05:22

I am trying to find the format \"abc, def g\" which is a name format \"lastname, firstname middlename\". I think the best suited method is regex but I do not have any idea i

5条回答
  •  被撕碎了的回忆
    2021-01-18 06:06

    As an alternative to matching the lastname, firstname middlename directly, you could use String.split and provide a regexp that matches the separators, instead. For instance:

    static String[] lastFirstMiddle(String input){
        String[] result=input.split("[,\\s]+");
        System.out.println(Arrays.asList(result));
        return result;
    }
    

    I tested this with inputs

    "Müller,   firstname  middlename"
    "Müller,firstname  middlename"
     "O'Gara, Ronan Ramón"
    

    Note: this approach fails with surnames that contain spaces, for instance "van der Heuvel", "de Valera", "mac Piarais" or "bin Laden" but then again, OP's original specification does not seem to admit of spaces in the surname (or the other names. I work with a "Mary Kate". That's her first name, not first and middle). There's an interesting page about personal names at http://www.w3.org/International/questions/qa-personal-names

提交回复
热议问题