简体   繁体   English

使用区分大小写字符的正则表达式拆分字符串上的名称

[英]Split names on a string with Regex with case sensitive characters

I want to split names into array with these kind of string.我想用这些字符串将名称拆分为数组。

Bol BolLouis King
Brandon ClarkeRui Hachimura
Michael Jeffery JordanDennis Rodman

to

['Bol Bol', 'Louis King']
['Brandon Clarke', 'Rui Hachimura']
['Michael Jeffery Jordan', 'Dennis Rodman']

I have already retied creating my own regex using ^[AZ]\\w+\\s[AZ][az]+ but this only matches the first name and i can't capture the 2nd or 3rd name.我已经使用^[AZ]\\w+\\s[AZ][az]+重新创建了我自己的正则表达式,但这仅匹配第一个名称,我无法捕获第二个或第三个名称。 I'm also having some issues when the name has 3 words on it like Michael Jeffery Jordan当名字有 3 个词时,我也遇到了一些问题,比如Michael Jeffery Jordan

I would suggest making use of a positive lookahead to be able to generalize your pattern.我建议使用积极的前瞻来概括您的模式。 That allows you to match an expression that is immediately followed by some other expression.这允许您匹配紧跟其他一些表达式的表达式。 Use a (?=someRegexp) at the end of your pattern to make the end be the case where a lowercase character is immediately followed by an uppercase one.在模式末尾使用 (?=someRegexp) 使结尾成为小写字符后紧跟大写字符的情况。 You can then generalize to any number of words.然后,您可以概括为任意数量的单词。

I would also suggest splitting it into two cases then, as the last name in your expression wouldn't be followed by a capital letter but rather by a end of string character.我还建议将其分为两种情况,因为表达式中的姓氏后面不会跟大写字母,而是跟在字符串结尾。 You can do that with an or: (someRegexp|someOtherRegexp)您可以使用 or 来做到这一点:(someRegexp|someOtherRegexp)

你没有说你想要这个,所以这里有一个 sed 版本,它适用于你的示例输入:

sed -e "s/\\(.*[az]\\)\\([AZ].*\\)/['\\1', '\\2']/g"

As not all browsers support lookbehind, here is a solution without:由于并非所有浏览器都支持后视,这里有一个没有以下解决方案:

 var test = [ 'Bol BolLouis King', 'Brandon ClarkeRui Hachimura', 'Michael Jeffery JordanDennis Rodman', ]; console.log(test.map(function (a) { // return a + ' :' + a.match(/\\b[az]{1,2}\\K\\s/); return a.match(/^(.+?[az])([AZ].+)/); }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM