简体   繁体   中英

How to find a pattern in a string

I am stuck while finding the pattern position in a string. The string is of 6 characters and consist of only uppercase letter(AZ) or digits (0-9) .

check_string = 'I have a string 09A4N5'

I have to find the position of '09A4N5' which could be any other string in a similar format. I tried with the regular expression and was able to find the get the below mentioned solution which gives the position of the word 'string' .

re.search('\w+\w+\w+\w+\w+\w',check_string).start()
9

You may use

m = re.search(r'\b[A-Z0-9]{6}\b', check_string)
if m:
    print(m.group())  # => 09A4N5
    print(m.start(0)) # => 16

See the Python demo and the regex demo .

Pattern details

  • \\b - a word boundary
    • [A-Z0-9] - an uppercase ASCII letter or digit
    • {6} - exactly six times
  • \\b - word boundary.

If you want to require at least one digit and at least one uppercase letter in the regex, use

r'\b(?=[A-Z]*\d)(?=\d*[A-Z])[A-Z0-9]{6}\b'

See the regex demo . Here,

  • (?=[AZ]*\\d) - requires at least one digit
  • (?=\\d*[AZ]) - requires at least one uppercase letter

Edit: Just saw it's always six characters.

You want to search for either AZ or 0-9 and combinations of those. With [..] you can define a character set:

re.findall(r"[A-Z0-9]{6}", check_string)

the {6} says, that you want exactly 6 characters. The position is found by search :

res = re.search(r"[A-Z0-9]{6}", check_string)
res.span()
check_string = 'I have a string 09A4N5'

for i, x in enumerate(check_string.split()):
    if len(x) < 6 or any([ i.islower() for i in x ]):
        continue
    else:
        idx = check_string.index(x)
        match = x

print('position:', idx, '- string:', match)
# position: 16 - string: 09A4N5

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