Determining the unmatched portion of a string using a regex in Python

前端 未结 4 1505
梦谈多话
梦谈多话 2021-01-12 12:50

Suppose I have a string \"a foobar\" and I use \"^a\\s*\" to match \"a \".

Is there a way to easily get \"foobar\" returned? (What was NOT matched)

I want to

4条回答
  •  梦谈多话
    2021-01-12 13:37

    from http://docs.python.org/library/re.html#re.split

    >>> re.split('(\W+)', 'Words, words, words.')
    ['Words', ', ', 'words', ', ', 'words', '.', '']
    

    so your example would be

    >>> re.split(r'(^a\s*)', "a foobar")
    ['', 'a ', 'foobar']
    

    at which point you can separate the odd items (your match) from the even items (the rest).

    >>> l = re.split(r'(^a\s*)', "a foobar")
    >>> l[1::2] # matching strings
    ['a ']
    >>> l[::2] # non-matching strings
    ['', 'foobar']
    

    This has the advantage over re.sub in that you can tell, when, where, and how many matches were found.

提交回复
热议问题