简体   繁体   中英

Python - REGEX issue with RE using function re.compile + search

I'm using regex library 're' in Python (2.7) to validate a flight number.

I've had no issues with expected outputs using a really helpful online editor here: http://regexr.com/

My results on regexr.com are: http://imgur.com/nB0QDug

My code is:

import re
test1 = 'ba116'
###Referencelink: http://academe.co.uk/2014/01/validating-flight-codes/
p = re.compile('/^([a-z][a-z]|[a-z][0-9]|[0-9][a-z])[a-z]?[0-9]{1,4}[a-z]?$/g')
m = p.search(test1)  # p.match() to find from start of string only
if m:
print 'It works!: ', m.group()  # group(1...n) for capture groups
else:
print 'Did not work'

I'm unsure why I get the 'didn't work' output where regexr shows one match (as expected)

I made a much simpler regex lookup, and it seemed that the results were correct, so it seems either my regex string is invalid, or I'm using re.complile (or perhaps the if loop) incorrectly?

'ba116' is valid, and should match.

Python's re.compile is treating your leading / and trailing /g as part of the regular expression, not as delimiters and modifiers. This produces a compiled RE that will never match anything, since you have ^ with stuff before it and $ with stuff after it.

The first argument to re.compile should be a string containing only the stuff you would put inside the slashes in a language that had /.../ regex notation. The g modifier corresponds to calling the findall method on the compiled RE; in this case it appears to be unnecessary. (Some of the other modifiers, eg i , s , m , correspond to values passed to the second argument to re.compile .)

So this is what your code should look like:

import re
test1 = 'ba116'
###Referencelink: http://academe.co.uk/2014/01/validating-flight-codes/
p = re.compile(r'^([a-z][a-z]|[a-z][0-9]|[0-9][a-z])[a-z]?[0-9]{1,4}[a-z]?$')
m = p.search(test1)  # p.match() to find from start of string only
if m:
    print 'It works!: ', m.group()  # group(1...n) for capture groups
else:
    print 'Did not work'

The r immediately before the open quote makes no difference for this regular expression, but if you needed to use backslashes in the RE it would save you from having to double all of them.

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