简体   繁体   中英

Simple Python Regex Matching

I am trying to match a string with a regular expression and only one of the two cases are working

String

ABCD 123 - Abcdef 0000
ABCD 123/DEFG123 - Abcdef 0000

RegEx

[A-Z]+ [A-Z0-9]{2,20} - [A-Z][a-z]+ [0-9]{4}

This matches the 1st one and Im trying to match it to both and this is the new expression I tried

[A-Z]+ [A-Z0-9\\/]{2,20} - [A-Z][a-z]+ [0-9]{4}

This matches DEFG123 - Abcdef 0000 out of the entire ABCD 123/DEFG123 - Abcdef 0000 but I am trying to get the entire string out of this. This is the code I'm using

regex = re.compile(expression)
r = regex.search(string)

I'm pretty sure you don't need to escape your forward slash. Remove the double \\ and you should be set. Use regexpal.com to test this kind of thing.

Remove the extra backslash:

[A-Z]+ [A-Z0-9\/]{2,20} - [A-Z][a-z]+ [0-9]{4}

Or you could use grouping:

([A-Z]+ [A-Z0-9]{2,20} - [A-Z][a-z]+ [0-9]{4})|([A-Z]+ [A-Z0-9\\/]{2,20} - [A-Z][a-z]+ [0-9]{4})

This combines both of the RegExes from above and matches both lines.

I've tried this code:

import re
def foo(s):
    r = re.compile('[A-Z]+ [A-Z0-9\\/]{2,20} - [A-Z][a-z]+ [0-9]{4}')
    m = r.search(s)
    return s[m.start():m.end()]
print foo('ABCD 123 - Abcdef 0000')
print foo('ABCD 123/DEFG123 - Abcdef 0000')

and got the following result:

ABCD 123 - Abcdef 0000
ABCD 123/DEFG123 - Abcdef 0000

Try to use ^ (start of line) and $ (end of line) chars:

r = re.compile('^[A-Z]+ [A-Z0-9\\/]{2,20} - [A-Z][a-z]+ [0-9]{4}$')

The escape is what's wrong. Taken out:

string_one = 'ABCD 123 - Abcdef 0000'
string_two = 'ABCD 123/DEFG123 - Abcdef 0000'
pattern = r'[A-Z]+ [A-Z0-9/]{2,20} - [A-Z][a-z]+ [0-9]{4}'
regex = re.compile(pattern)
if regex.search(string_one):
    print "string one matches!"
if regex.search(string_two):
    print "string two matches!"

# output:
string one matches!
string two matches!

Note that my use of a raw string ( r'this is a raw string' ) is unnecessarily, but I obsessively use raw strings for patterns because they prevent most backslash interpolation.

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