“我正在尝试使用正则表达式删除以小写字母开头的单词,但没有得到所需的输出。”
我的输入是“适用于此法案,并已成为Illiam B GEISSLER的一部分”
import re
text = "apply to this bill and are made a part thereof Illam B GEISSLER"
result = re.sub(r"\w[a-z]", "", text)
print(result)
我得到的输出为“ I B GEISSLER” 要求输出为“ Illiam B GEISSLER”
答案 0 :(得分:3)
尝试找到模式INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'django_mysql',
]
,并替换为空字符串:
\b[a-z]+\s*
此打印:
text = "apply to this bill and are made a part thereof Illam B GEISSLER"
result = re.sub(r'\b[a-z]+\s*', "", text).strip()
print(result)
模式Illam B GEISSLER
背后的想法是,它仅与两侧都被单词边界包围的整个单词相匹配。请注意,我们调用\b[a-z]+\s*
删除所有剩余的空格。
另一个微妙之处是该模式删除了每个匹配的小写字母的RHS上的所有空格。这是为了使文本可读,例如,某些匹配的单词应位于某些不匹配的单词之间:
strip
这可以正确打印:
text = "United States Of a bunch of states called America"
result = re.sub(r'\b[a-z]+\s*', "", text).strip()
print(result)
答案 1 :(得分:1)
您可以搜索大写单词 在链接中可以找到示例
答案 2 :(得分:1)
尝试一下
import re
text = "apply to this bill and are made a part thereof Illam B GEISSLER"
result = re.sub(r"(\b[a-z]+)", '', text).strip()
print(result)
输出
Illam B GEISSLER
答案 3 :(得分:0)
此表达式也可能起作用:
\s*\b[a-z][a-z]*
import re
regex = r"\s*\b[a-z][a-z]*"
test_str = "apply to this bill and are made a part thereof Illam B GEISSLER apply to this bill and are made a part thereof Illam B GEISSLER"
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
或者这一个:
([A-Z].*?\b\s*)
import re
regex = r"([A-Z].*?\b\s*)"
test_str = "apply to this bill and are made a part thereof Illam B GEISSLER apply to this bill and are made a part thereof Illam B GEISSLER"
print("".join(re.findall(regex, test_str)))
Illam B GEISSLER Illam B GEISSLER