简体   繁体   English

查找以大写字母作为起始字母但前面没有空格的单词

[英]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.

相关问题 在2个大写字母(regex)之前找到以大写字母开头的n个单词 - Find n words starting with capital letter before 2 words of capital letters (regex) Pythonic句子拆分以大写字母开头的单词 - Pythonic sentence splitting on words starting with capital letter 组合列表中的字符串以形成以大写字母开头的单词 - Combine strings in list to form words starting with capital letter 在大写字母之间添加空格,但忽略 3 个字母的单词 - Add space in-between Capital Letters but ignore 3-letter words 如何查找单词 - 第一个字母大写,其他字母小写 - How to find a words - First letter will be capital & other will be lower 如何使用正则表达式从 NLTK 语料库中查找大写字母单词? - How to find a capital letter words from an NLTK corpus using regex? 如何找到以大写字母开头的字符串中的单词? - how can i find the words in a string that start with capital letter? 如果大写字母前面和后面跟着一个小写字母,则插入空格 - Python - Insert space if uppercase letter is preceded and followed by one lowercase letter - Python 正则表达式将单词与首字母大写匹配 - Regex to match words with first capital letter 如何使用 python 中的 re.sub 删除字符串列表中以大写字母开头的单词 - How to remove words starting with capital letter in a list of strings using re.sub in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM