python ply语法错误,无法解析d[0-9]+
python ply syntax error,can't parse d[0-9]+
我正在尝试解析此列表:
d0,d1,d2,d3,....d456,d457....
为了在 python-ply 中解析它,我将其写为表达式:
t_DID = r'[d][0-9]+'
t_DID = r'd[0-9]+'
t_DID = r'\d[0-9]+'
但是,它给我错误。
当我输入 1 时,它会给我 - DEBUG:root:'1'处的语法错误
当我输入 d 时,它给我 - DEBUG:root:'d'
处的语法错误
对于这个模式,正确的标记是什么?
我该如何解决这个问题?
None 这些模式匹配 d
或 1
。
r'[d][0-9]+'
和 r'd[0-9]+'
匹配 d
后跟至少一位数字。所以它们将匹配 d1
或 d234
,但它们不会匹配 d
因为它后面没有数字,它们不会匹配 1
因为它没有'以 d
开头
r'\d[0-9]+'
匹配一个数字 (\d
) 后跟至少一个数字。所以它不会匹配任何以 d
开头的字符串,也不会匹配 1
因为它至少需要两位数字。但它将匹配 12
、274
和 29847502948375029384750293485702938750493875
.
您可以在 Python docs (The \
escape codes, including \d
, are here).
中阅读有关 Python 正则表达式的内容
构建交互式工具很容易,您可以使用 Python 正则表达式进行试验。这是一个非常简单的例子,可以改进很多:
$ python3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> import readline
>>> def try_regex(regex):
... r = re.compile(regex)
... try:
... while True:
... match = r.match(input('--> '))
... if match:
... print(f"Matched {match.end()} characters: {match[0]}")
... else:
... print("No match")
... except EOFError:
... pass
...
>>> try_regex(r'd[0-9]+')
--> d1
Matched 2 characters: d1
--> d123
Matched 4 characters: d123
--> 1
No match
--> d
No match
--> d123 abc
Matched 4 characters: d123
--> d123abc
Matched 4 characters: d123
我正在尝试解析此列表:
d0,d1,d2,d3,....d456,d457....
为了在 python-ply 中解析它,我将其写为表达式:
t_DID = r'[d][0-9]+'
t_DID = r'd[0-9]+'
t_DID = r'\d[0-9]+'
但是,它给我错误。
当我输入 1 时,它会给我 - DEBUG:root:'1'处的语法错误
当我输入 d 时,它给我 - DEBUG:root:'d'
处的语法错误对于这个模式,正确的标记是什么?
我该如何解决这个问题?
None 这些模式匹配 d
或 1
。
r'[d][0-9]+'
和r'd[0-9]+'
匹配d
后跟至少一位数字。所以它们将匹配d1
或d234
,但它们不会匹配d
因为它后面没有数字,它们不会匹配1
因为它没有'以d
开头
r'\d[0-9]+'
匹配一个数字 (\d
) 后跟至少一个数字。所以它不会匹配任何以d
开头的字符串,也不会匹配1
因为它至少需要两位数字。但它将匹配12
、274
和29847502948375029384750293485702938750493875
.
您可以在 Python docs (The \
escape codes, including \d
, are here).
构建交互式工具很容易,您可以使用 Python 正则表达式进行试验。这是一个非常简单的例子,可以改进很多:
$ python3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> import readline
>>> def try_regex(regex):
... r = re.compile(regex)
... try:
... while True:
... match = r.match(input('--> '))
... if match:
... print(f"Matched {match.end()} characters: {match[0]}")
... else:
... print("No match")
... except EOFError:
... pass
...
>>> try_regex(r'd[0-9]+')
--> d1
Matched 2 characters: d1
--> d123
Matched 4 characters: d123
--> 1
No match
--> d
No match
--> d123 abc
Matched 4 characters: d123
--> d123abc
Matched 4 characters: d123