I am trying to use Python v2 (2.7.5 specifically) and the 're' module for regex matching. My problem is that for my application I need to match the 'space' symbol (i.e. 0x20 in hex) and ONLY that symbol as part of the match string. The first thing I tried for that was '\s' and that does not work because it also matches the newline, return, tab and form.
我正在尝试使用Python v2(特别是2.7.5)和're'模块进行正则表达式匹配。我的问题是,对于我的应用程序,我需要匹配'空格'符号(即十六进制的0x20)和仅作为匹配字符串的一部分的符号。我尝试的第一件事是'\ s',但这不起作用,因为它也匹配换行,返回,制表符和表单。
The end requirement is to match a string where the first three characters are digits ('\d'), there is a comma (',') and then eight symbols that are either digits ('\d') or spaces (???).
最终要求是匹配一个字符串,其中前三个字符是数字('\ d'),有一个逗号(','),然后是八个符号,它们是数字('\ d')或空格(?? ?)。
Any suggestions on how to do that? What I have already tried...
有关如何做到这一点的任何建议?我已经尝试过的......
C:\Users\jlaird>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> goodstring = '333,000000 2'
>>> badstring = '333,000000\t2'
>>> print badstring
333,000000 2
>>> sRegex = '\d\d\d,[\s\d][\s\d][\s\d][\s\d][\s\d][\s\d][\s\d][\s\d]'
>>> cRegex = re.compile(sRegex)
>>> cRegex.match(goodstring)
<_sre.SRE_Match object at 0x023A7A30>
>>> cRegex.match(badstring)
<_sre.SRE_Match object at 0x025E82C0>
>>>
I want 'badstring' to evaluate to None because it has the tab character instead of the space. How can I do this?
我希望'badstring'评估为None,因为它有制表符而不是空格。我怎样才能做到这一点?
1 个解决方案
#1
1
Thanks jonrsharpe...works. It is always something simple that I make complicated. Sorry...
谢谢jonrsharpe ...工作。我制作复杂的东西总是很简单。抱歉...
>>> sRegex = '\d\d\d,[ \d][ \d][ \d][ \d][ \d][ \d][ \d][ \d]'
>>> cRegex = re.compile(sRegex)
>>> cRegex.match(goodstring)
<_sre.SRE_Match object at 0x023A7A30>
>>> cRegex.match(badstring)
>>>
#1
1
Thanks jonrsharpe...works. It is always something simple that I make complicated. Sorry...
谢谢jonrsharpe ...工作。我制作复杂的东西总是很简单。抱歉...
>>> sRegex = '\d\d\d,[ \d][ \d][ \d][ \d][ \d][ \d][ \d][ \d]'
>>> cRegex = re.compile(sRegex)
>>> cRegex.match(goodstring)
<_sre.SRE_Match object at 0x023A7A30>
>>> cRegex.match(badstring)
>>>