Yan*_*roy 5 python regex whitespace integer match
我找不到只包含空格或整数的字符串的正则表达式。该字符串是用户在键盘上的输入。它可以包含所有内容,但\n(但我猜这无关紧要),但我们可以专注于 ASCII,因为它应该是英语句子 这里有一些例子:
好的:
'1'
'2 3'
' 3 56 '
'8888888 333'
' 039'
Run Code Online (Sandbox Code Playgroud)
不好:
'a'
'4 e'
'874 1231 88 qqqq 99'
' shf ie sh f 8'
Run Code Online (Sandbox Code Playgroud)
我有这个可以找到数字:
t = [int(i) for i in re.findall(r'\b\d+\b', text)]
Run Code Online (Sandbox Code Playgroud)
但我无法获得正则表达式。我的正则表达式目前是,re.match(r'(\b\d+\b)+', text)但它不起作用。
Mar*_*ark 15
>>> re.match(r'^([\s\d]+)$', text)
Run Code Online (Sandbox Code Playgroud)
需要将开始(^)和行尾($)字符放入,否则字符串中包含字符的部分将匹配,导致误报匹配