繁体   English   中英

如何使用Python Regex查找所有后跟符号的单词?

[英]How to find all words followed by symbol using Python Regex?

我需要re.findall来检测后跟a "="单词

所以它适用于一个例子

re.findall('\w+(?=[=])', "I think Python=amazing")

但不适用于“我认为Python =令人惊叹”或“ Python =令人惊叹” ...我不知道如何在此处正确集成空白问题。

谢谢一群!

您说“再次卡在正则表达式中”可能是参考您先前的问题寻找在脚本标识和替换Python变量的方法,在该脚本中您可以找到所要问题的答案,但我不认为您是在问这个问题您真的想要答案。

您正在寻找重构Python代码的方法,除非您的工具能理解 Python,否则它将产生假阳性和假阴性。 也就是说,查找variable =实例,这些实例不是您的正则表达式不匹配的分配和丢失的分配。

您可以使用Python的哪些重构工具来部分列出工具 并且使用“重构Python your_editing_environment”进行更多常规搜索将产生更多结果。

'(\w+)\s*=\s*'
re.findall('(\w+)\s*=\s*', 'I think Python=amazing')   \\ return 'Python'
re.findall('(\w+)\s*=\s*', 'I think Python = amazing') \\ return 'Python'
re.findall('(\w+)\s*=\s*', 'I think Python =amazing')  \\ return 'Python'

只需在=之前添加一些可选的空格:

\w+(?=\s*=)

改用这个

 re.findall('^(.+)(?=[=])', "I think Python=amazing")

说明

# ^(.+)(?=[=])
# 
# Options: case insensitive
# 
# Assert position at the beginning of the string «^»
# Match the regular expression below and capture its match into backreference number 1 «(.+)»
#    Match any single character that is not a line break character «.+»
#       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
# Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[=])»
#    Match the character “=” «[=]»

您需要在单词和=之间留出空格:

re.findall('\w+(?=\s*[=])', "I think Python = amazing")

您还可以通过在单词周围使用捕获组而不是在等号周围使用非捕获组来简化表达式:

re.findall('(\w+)\s*=', "I think Python = amazing")

r'(.*)=.*'也可以做到...

您有#1后面跟有= ,再有#2后面有任何东西,您得到了#1。

>>> re.findall(r'(.*)=.*', "I think Python=amazing")
['I think Python']
>>> re.findall(r'(.*)=.*', "  I think Python =    amazing oh yes very amazing   ")
['  I think Python ']
>>> re.findall(r'(.*)=.*', "=  crazy  ")
['']

然后,您可以strip()返回列表中的字符串。

re.split(r'\s*=', "I think Python=amazing")[0].split() # returns ['I', 'think', 'Python']

暂无
暂无

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

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