Need help moving a middle initial from the end of a name to the middle using java regex -
i made topic on before , got closed , labeled off-topic. tried reading faqs couldn't figure out why pardon me if off-topic had additional questions on issue.
i have field contains full name in following format: first, last (mi.)
challenges:
the middle initial isn't present entries don't have middle initial listed.
some middle initials have period @ end.
many of names spanish names can have multiple first , last names splitting field space isn't option.
the name field may have white spaces @ end.
the middle name may contain accented character (i couldn't find spanish middle names did start accent couldn't sure wouldn't have any).
what i've done far:
the first thing did used trim() remove padding @ end of field.
fname.trim()
next, i've split name first , last names following:
string[] names = fname.split(",");
i need split middle name last name, can following:
fname = names[0] + " " names [1] + " " + names [2]
what i'm stuck on:
i need use regex check last name , see if last character period. if need check see if characters before space + letter (might accented). if there isn't period still need check if there space + letter.
for example:
john, doe f --> john f doe john, doe f. --> john f. doe
i need remove comma later that's easy fix doing fname.replace(",","")
some more challenging examples of names:
victor, ramirez-briano m felix, del valle-ortiz g. sandra, de leon mendoza maria, hernandez-de la torre isabel j. carlos armando, perez-fernandez l j. concepcion, rodriguez-balderas miguel a, luzuriaga-alvarez
you can in 1 line:
name = name.replaceall(",(.*?)( \\w\\.?)?$", "$2$1");
this works whether or not middle initial present @ end.
here's test code using above line examples question:
string[] names = { "john, doe f", "john, doe f.", "john, doe", "victor, ramirez-briano m", "felix, del valle-ortiz g.", "sandra, de leon mendoza a", "maria, hernandez-de la torre isabel j.", "carlos armando, perez-fernandez l", "j. concepcion, rodriguez-balderas", "miguel a, luzuriaga-alvarez" }; (string name : names) system.out.println(name + " --> " + name.replaceall(",(.*?)( \\w\\.?)?$", "$2$1"));
output:
john, doe f --> john f doe john, doe f. --> john f. doe john, doe --> john doe victor, ramirez-briano m --> victor m ramirez-briano felix, del valle-ortiz g. --> felix g. del valle-ortiz sandra, de leon mendoza --> sandra de leon mendoza maria, hernandez-de la torre isabel j. --> maria j. hernandez-de la torre isabel carlos armando, perez-fernandez l --> carlos armando l perez-fernandez j. concepcion, rodriguez-balderas --> j. concepcion rodriguez-balderas miguel a, luzuriaga-alvarez --> miguel luzuriaga-alvarez
Comments
Post a Comment