[英]find words with capital letter as starting letter but not preceded by space
I am trying to find solution to find words in a text, must start with capital letter but not preceded by a space.我正在尝试找到在文本中查找单词的解决方案,必须以大写字母开头,但前面不能有空格。 example:
例子:
string1 = "MynameisStuartLittle" # expected result ["Mynameis","Stuart","Little"]
string2 = "MynameisStuart Little Junior" # expected result ["Mynameis","Stuart"]
string3 = "My name is AlphredHitchcock" # expected result ["My","Hitchcock"]
result = re.findall(r"([^ ]([A-Z][a-z]+))",string1)
print(result)
Other alternative solution I am thinking is that to split the text by SPACE and individually check each word with regex r"([AZ][az]+)" then if if length of findall is more than 1, then that word is eligible for my result.我正在考虑的其他替代解决方案是按空格分割文本并使用正则表达式 r"([AZ][az]+)" 单独检查每个单词,然后如果 findall 的长度大于 1,则该单词有资格我的结果。 I am looking for any single regex solution.
我正在寻找任何单一的正则表达式解决方案。
You can use negative lookbehind (?<....)
:您可以使用否定的lookbehind
(?<....)
:
import re
string1 = "MynameisStuartLittle"
string2 = "MynameisStuart Little Junior"
string3 = "My name is AlphredHitchcock"
print(re.findall(r"(?<! )[A-Z][a-z]*", string1)) # ['Mynameis', 'Stuart', 'Little']
print(re.findall(r"(?<! )[A-Z][a-z]*", string2)) # ['Mynameis', 'Stuart']
print(re.findall(r"(?<! )[A-Z][a-z]*", string3)) # ['My', 'Hitchcock']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.