Regex, split all last names into single group

时间:2021-07-19 21:44:19

I need to split a full name. I know that this question has been asked again and again, but I cannot find the solution that I need.

我需要分割一个全名。我知道这个问题一再被问到,但我找不到我需要的解决方案。

https://regex101.com/r/PvDIPA/1

I have a full name: 'Firstname Middlename Lastname'

我有一个全名:'Firstname Middlename Lastname'

And I need all except the first name to be put in to group 1

我需要将除名字之外的所有名字放入第1组

I have tried this RegEx: (\s+\S*)$ But it only put's the 'Lastname' in group 1, I need 'Middlename Lastname' to be in group 1.

我已经尝试了这个RegEx:(\ s + \ S *)$但它只是在组1中放置'姓氏',我需要'Middlename Lastname'在组1中。

I hope you can help.

我希望你能帮忙。

2 个解决方案

#1


0  

Are you looking for something as simple as: (\s+\S*){2}$? https://regex101.com/r/PvDIPA/2

你在找一些简单的东西:(\ s + \ S *){2} $? https://regex101.com/r/PvDIPA/2

Note that this only works if someone has one middle name.

请注意,这只适用于某人有一个中间名称的情况。

Perhaps this suits you better: ^\w+((?:\h+\w+)+)$ https://regex101.com/r/PvDIPA/4

也许这更适合你:^ \ w +((?:\ h + \ w +)+)$ https://regex101.com/r/PvDIPA/4

Note that I have used \h here instead of \s since I am using regex101 with //gm flags, so \s would have also matched the end-of-line resulting in the first match also eating the second line.

请注意,我在这里使用\ h而不是\ s,因为我使用带有// gm标志的regex101,所以\ s也会匹配行尾,导致第一个匹配也吃第二行。

#2


0  

[^ ]+ +((?:\w+ *){1,2})

[^] + +((?:\ w + *){1,2})

See demo

Usage

[^ ]+ + match everything up to and including the first occurrences of a space character

[^] + +匹配所有内容,包括第一次出现的空格字符

((?:\w+ *){1,2}) capture 1 or 2 occurrences of any alpha-numeric chars followed by zero or more space chars (the outer brackets are the capturing group, the inner brackets with ?: are a non-capturing group See the more full explanation at the demo page)

((?:\ w + *){1,2})捕获1或2次出现的任何字母数字字符,后跟零个或多个空格字符(外部括号为捕获组,内部括号为?:为非-capturing group在演示页面查看更完整的说明)

Also allows for more than 1 space to be present between the names

还允许名称之间存在多于1个空格

#1


0  

Are you looking for something as simple as: (\s+\S*){2}$? https://regex101.com/r/PvDIPA/2

你在找一些简单的东西:(\ s + \ S *){2} $? https://regex101.com/r/PvDIPA/2

Note that this only works if someone has one middle name.

请注意,这只适用于某人有一个中间名称的情况。

Perhaps this suits you better: ^\w+((?:\h+\w+)+)$ https://regex101.com/r/PvDIPA/4

也许这更适合你:^ \ w +((?:\ h + \ w +)+)$ https://regex101.com/r/PvDIPA/4

Note that I have used \h here instead of \s since I am using regex101 with //gm flags, so \s would have also matched the end-of-line resulting in the first match also eating the second line.

请注意,我在这里使用\ h而不是\ s,因为我使用带有// gm标志的regex101,所以\ s也会匹配行尾,导致第一个匹配也吃第二行。

#2


0  

[^ ]+ +((?:\w+ *){1,2})

[^] + +((?:\ w + *){1,2})

See demo

Usage

[^ ]+ + match everything up to and including the first occurrences of a space character

[^] + +匹配所有内容,包括第一次出现的空格字符

((?:\w+ *){1,2}) capture 1 or 2 occurrences of any alpha-numeric chars followed by zero or more space chars (the outer brackets are the capturing group, the inner brackets with ?: are a non-capturing group See the more full explanation at the demo page)

((?:\ w + *){1,2})捕获1或2次出现的任何字母数字字符,后跟零个或多个空格字符(外部括号为捕获组,内部括号为?:为非-capturing group在演示页面查看更完整的说明)

Also allows for more than 1 space to be present between the names

还允许名称之间存在多于1个空格