- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试匹配输出变量中的数据,正在寻找匹配 *
之后的单词,正在尝试以下方式但遇到错误,如何解决?
import re
output = """test
* Peace
master"""
m = re.search('* (\w+)', output)
print m.group(0)
错误:-
Traceback (most recent call last):
File "testinglogic.py", line 7, in <module>
m = re.search('* (\w+)', output)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 146, in search
return _compile(pattern, flags).search(string)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 251, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat
最佳答案
第一个修复方法是转义 *
,因为您希望引擎按字面意思(作为星号)对待它,所以您使用反斜杠转义它。
另一个建议是使用lookbehind,所以你不需要使用另一个捕获组:
>>> re.search('(?<=\*\s)\w+', output).group()
'Peace'
关于python - 无效的表达式 sre_constants.error : nothing to repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48837759/
我正在使用正则表达式,但出现错误: Traceback (most recent call last): File "tokennet.py", line 825, in RunIt(C
我正在使用正则表达式,但出现错误: Traceback (most recent call last): File "tokennet.py", line 825, in RunIt(C
我有html内容我想从这个内容中获得评论 content = """ test--> --> Blockquote --> """ 我使用这个正则表达式 rege
我正在尝试匹配输出变量中的数据,正在寻找匹配 * 之后的单词,正在尝试以下方式但遇到错误,如何解决? import re output = """test * Peace
我在使用看似简单的 Python 正则表达式时遇到了问题。 # e.g. If I wanted to find "mark has wonderful kittens, but they're mi
以下是错误信息: Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/re.py"
我在从 pandas 数据框中删除停用词时遇到问题。我的代码是这样的: for word in stopwords: df['name'] = df['name'].str.replace(wor
我正在尝试使用此正则表达式从另一个字符串获取 JSON 字符串: YAHOO.Finance.SymbolSuggest.ssCallback\((.*?)\) 它适用于 regex101.com ,
我试图匹配 + 之间的所有子字符串和 h ,例如 +abcd1234h . 代码如下: match = re.match(r'.*+(.*)h.*',line) 当我运行我的代码时,发生如下错误: F
所以我正在对某些东西进行一些测试,我需要一种将字符串分成两个组的方法。 (例如 'abcdef' => ['ab','cd','ef']) 我正在尝试使用正则表达式模式来执行此操作 ([^]{2})。
我写了一个Python正则表达式。 import re nt = re.sub('NT:\s*[^,)]*',r'\1', message) 消息包含数据。 (NT:167 毫秒,ST:2509 毫秒
这个错误的原因可能是什么? File "/pga/mcad/install/python/python-2.7.6.9/linux/RH6/lib/python2.7/multiprocessing/
这是我的正则表达式: (?P=[a-zæøå ]+)(?:[ ]+)(?P=\d+)(?:[ ]+),(?:[ ]+)(?P=\d{1,4})(?:[ ]+)(?P=[a-zæøå ]+) 所有组名都
我正在使用 Pandas 。我已经下载了成人数据集并将其转换为 csv 文件,现在我尝试使用 read_csv 函数读取 csv 文件,但出现以下错误: sre_constants.error: n
我知道已经有几个关于这个主题的问题,但没有一个能帮助我解决我的问题。 我必须替换 CSV 文档中的姓名,当他们跟随标签 {SPEAKER} 或 {GROUP OF SPEAKERS} 时。 代码 我的
我是一名优秀的程序员,十分优秀!