1 回答

TA貢獻1797條經驗 獲得超6個贊
OP 的評論讓我意識到我的錯誤:預期的庫是regex,而不是 python 的 builtin re。
使用import regex as re不僅讓我感到困惑,而且還導致re庫拋出nothing to repeat錯誤。這是因為默認情況下 Dataflow 不會保存您的主會話。
當您的解析函數中的代碼正在執行時,它無法訪問re您在構建時導入的上下文。通常,這會失敗NameError,但由于您使用的是有效的庫名稱,因此代碼假定您指的是內置re庫并嘗試按原樣執行它。
如果您import regex改用,您會看到NameError: name 'regex' is not defined,這是代碼失敗的真正原因。為了解決這個問題,要么將導入語句移動到解析函數本身,要么--save_main_session作為選項傳遞給運行程序。有關更多詳細信息,請參見此處。
老答案:
雖然我不知道您使用的是哪個版本的 Python,但您對正則表達式的懷疑似乎是正確的。 *是一個特殊字符,表示它之前的重復,但它(是一個表示分組的特殊字符,所以類似的模式(*SKIP)在語法上似乎不正確。
在 Python 3.7 中,上述表達式甚至無法編譯:
python -c 'import re; rx = re.compile(r"""\{[^{}]+\}(*SKIP)(*FAIL)|,""")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/ajp1/miniconda3/envs/b-building/lib/python3.7/re.py", line 234, in compile
return _compile(pattern, flags)
File "/home/ajp1/miniconda3/envs/b-building/lib/python3.7/re.py", line 286, in _compile
p = sre_compile.compile(pattern, flags)
File "/home/ajp1/miniconda3/envs/b-building/lib/python3.7/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/home/ajp1/miniconda3/envs/b-building/lib/python3.7/sre_parse.py", line 930, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File "/home/ajp1/miniconda3/envs/b-building/lib/python3.7/sre_parse.py", line 426, in _parse_sub
not nested and not items))
File "/home/ajp1/miniconda3/envs/b-building/lib/python3.7/sre_parse.py", line 816, in _parse
p = _parse_sub(source, state, sub_verbose, nested + 1)
File "/home/ajp1/miniconda3/envs/b-building/lib/python3.7/sre_parse.py", line 426, in _parse_sub
not nested and not items))
File "/home/ajp1/miniconda3/envs/b-building/lib/python3.7/sre_parse.py", line 651, in _parse
source.tell() - here + len(this))
re.error: nothing to repeat at position 11
Python 2.7.15 也不接受它:
python2 -c 'import re; rx = re.compile(r"""\{[^{}]+\}(*SKIP)(*FAIL)|,""")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/re.py", line 194, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 251, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat
雖然我不知道您要匹配哪些字符串,但我懷疑您的某些字符需要轉義。例如"\{[^{}]+\}(\*SKIP)(\*FAIL)|,"
添加回答
舉報