>>> import re >>> pattern = re.compile(r'*.h') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python3.6/re.py", line 233, in compile return _compile(pattern, flags) File "/usr/lib64/python3.6/re.py", line 301, in _compile p = sre_compile.compile(pattern, flags) File "/usr/lib64/python3.6/sre_compile.py", line 562, in compile p = sre_parse.parse(p, flags) File "/usr/lib64/python3.6/sre_parse.py", line 855, in parse p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0) File "/usr/lib64/python3.6/sre_parse.py", line 416, in _parse_sub not nested and not items)) File "/usr/lib64/python3.6/sre_parse.py", line 616, in _parse source.tell() - here + len(this)) sre_constants.error: nothing to repeat at position 0 >>> pattern = re.compile(r'.*.h')
实验
回顾这个语句
1
grep -E'*.h$|*.c$|*.hpp$|*.cpp$|*.cc$'
显然这不是一个合法的正则表达式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
>>> pattern = re.compile(r'*.h$|*.c$|*.hpp$|*.cpp$|*.cc$') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python3.6/re.py", line 233, in compile return _compile(pattern, flags) File "/usr/lib64/python3.6/re.py", line 301, in _compile p = sre_compile.compile(pattern, flags) File "/usr/lib64/python3.6/sre_compile.py", line 562, in compile p = sre_parse.parse(p, flags) File "/usr/lib64/python3.6/sre_parse.py", line 855, in parse p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0) File "/usr/lib64/python3.6/sre_parse.py", line 416, in _parse_sub not nested and not items)) File "/usr/lib64/python3.6/sre_parse.py", line 616, in _parse source.tell() - here + len(this)) sre_constants.error: nothing to repeat at position 0