开发者

Python regex does not search with group numbering

I am trying out the example given in http://docs.python.or开发者_StackOverflow中文版g/library/re.html for the group numbering and matching. Very simply, (.+) \1 should match two pieces of identical text separated by a space, and it lists the the as an example. However,

 re.search('(.+) \1','the the')

returns a None.

I am using re version 2.2.1.


'\1' is '\x01'. Perhaps you meant '(.+) \\1' or r'(.+) \1'.


You either need to escape your escapes or use raw strings. e.g.

re.search('(.+) \\1', 'the the' )

or

re.search(r'(.+) \1', 'the the' )

see the Raw String Notation section on the same page as the example.


I would recommend using named groups like these:

(?P=name)

So in this case I would recommend using named patterns and doing this:

re.search('(?P<first_match>.+) (?P=first_match)', 'the the')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜