简体   繁体   中英

Python regex match whole string only

Is there any easy way to test whether a regex matches an entire string in Python? I thought that putting $ at the end would do this, but it turns out that $ doesn't work in the case of trailing newlines.

For example, the following returns a match, even though that's not what I want.

re.match(r'\w+$', 'foo\n')

You can use \\Z :

\\Z

Matches only at the end of the string.

In [5]: re.match(r'\w+\Z', 'foo\n')

In [6]: re.match(r'\w+\Z', 'foo')
Out[6]: <_sre.SRE_Match object; span=(0, 3), match='foo'>

You can use a negative lookahead assertion to require that the $ is not followed by a trailing newline:

>>> re.match(r'\w+$(?!\n)', 'foo\n')
>>> re.match(r'\w+$(?!\n)', 'foo')
<_sre.SRE_Match object; span=(0, 3), match='foo'>

re.MULTILINE is not relevant here; OP has it turned off and the regex is still matching. The problem is that $ always matches right before the trailing newline :

When [ re.MULTILINE is] specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline). By default, '^' matches only at the beginning of the string, and '$' only at the end of the string and immediately before the newline (if any) at the end of the string.

I have experimentally verified that this works correctly with re.X enabled.

To test whether you matched the entire string, just check if the matched string is as long as the entire string:

m = re.match(r".*", mystring)
start, stop = m.span()
if stop-start == len(mystring):
    print("The entire string matched")

Note: This is independent of the question (which you didn't ask) of how to match a trailing newline.

Based on @alexis answer: A method to check for a fullMatch could look like this:

def fullMatch(matchObject, fullString):
    if matchObject is None:
        return False
    start, stop = matchObject.span()
    return stop-start == len(fullString):

Where the fullString is the String on which you apply the regex and the matchObject is the result of matchObject = re.match(yourRegex, fullString)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM