regex to parse first and last name

孤者浪人 提交于 2019-12-24 11:40:10

问题


I parse some logs and in some case user name shown as FirstName.LastName in the other cases it shown as FLastName. I am just wonder if it is possible to parse both to names to FLastName. For example Joe.Doe and JDoe should both yeild JDoe. Thank you.


回答1:


This will do it:

(.)(?:[^\.]*\.)(.+)$

Basically it grabs the first character and then allows for multiple characters followed by a dot and then grabs the rest. The replacement string would be:

$1$2

But that depends on your regex tool that you're using.

Try it on RegExr (thanks stema, I didn't know about this site).




回答2:


Not sure what regex platform you are using but this will work in sed:

sed 's/\([a-z[A-Z]\).*\.\(.*\)$/\1\2/'

For strings "FirstName.LastName and FLastName it gives output:

FLastName



回答3:


My solution, (but the other two will work as well)

^([a-zA-Z])(?:.*\.)?(.*)$

See online here on Regexr

It matches the first letter, then the following part till the dot is optional. At last matches the string till the end. The first letter is in group 1 and the LastName is in group 2.

So replace with $1$2 (or \1\2, depending on your regex engine)



来源:https://stackoverflow.com/questions/5994870/regex-to-parse-first-and-last-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!