Check if string matches pattern

721,282

Solution 1

import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)

Solution 2

One-liner: re.match(r"pattern", string) # No need to compile

import re
>>> if re.match(r"hello[0-9]+", 'hello1'):
...     print('Yes')
... 
Yes

You can evalute it as bool if needed

>>> bool(re.match(r"hello[0-9]+", 'hello1'))
True

Solution 3

Please try the following:

import re

name = ["A1B1", "djdd", "B2C4", "C2H2", "jdoi","1A4V"]

# Match names.
for element in name:
     m = re.match("(^[A-Z]\d[A-Z]\d)", element)
     if m:
        print(m.groups())

Solution 4

import re
import sys

prog = re.compile('([A-Z]\d+)+')

while True:
  line = sys.stdin.readline()
  if not line: break

  if prog.match(line):
    print 'matched'
  else:
    print 'not matched'

Solution 5

  
import re

ab = re.compile("^([A-Z]{1}[0-9]{1})+$")
ab.match(string)
  


I believe that should work for an uppercase, number pattern.

Share:
721,282
DanielTA
Author by

DanielTA

Updated on September 22, 2021

Comments

  • DanielTA
    DanielTA over 2 years

    How do I check if a string matches this pattern?

    Uppercase letter, number(s), uppercase letter, number(s)...

    Example, These would match:

    A1B2
    B10L1
    C1N200J1
    

    These wouldn't ('^' points to problem)

    a1B2
    ^
    A10B
       ^
    AB400
    ^
    
  • conradkleinespel
    conradkleinespel over 7 years
    From the docs on re.match: If zero or more characters at the beginning of string match the regular expression pattern. I just spent like 30 minutes trying to understand why I couldn't match something at the end of a string. Seems like it's not possible with match, is it? For that, re.search(pattern, my_string) works though.
  • CrazyCasta
    CrazyCasta over 7 years
    @conradk Yes, you're right, I think there's something like an implied ^ at the beginning when you use match. I think it's a bit more complicated then that very simple explanation, but I'm not clear. You are correct that it does start from the beginning of the string though.
  • Rick Smith
    Rick Smith over 7 years
    This is the only case that returns the match which is required for getting groups. Best answer in my opinion.
  • LondonRob
    LondonRob about 6 years
    That's weird. Why can you use re.match in the context of an if, but you have to use bool if you're using it elsewhere?
  • LondonRob
    LondonRob about 6 years
    Careful with re.match. It only matches at the start of a string. Have a look at re.search instead.
  • Dennis
    Dennis about 5 years
    @LondonRob probably because if checks for the match not being None.
  • Suh Fangmbeng
    Suh Fangmbeng about 4 years
    There's a big need to compile to make sure there are no errors in the regular expressions like bad character range errors
  • nehem
    nehem about 4 years
    @SuhFangmbeng Compilation is useful when the same re is used in more than one places to improve efficiency. In terms of error .match would throw the same error what .compile does. It's perfectly safe to use.
  • Robo Robok
    Robo Robok about 3 years
    I edited your answer, because it only makes sense with search() in this context.
  • miksus
    miksus almost 3 years
    @nehem actually all of the regex functions in re module compile and cache the patterns. Therefore there is absolutely no efficiency gain using compile and then match than just directly calling re.match. All of these functions call the internal function _compile (including re.compile) which does the caching to a python dictionary.
  • CrazyCasta
    CrazyCasta over 2 years
    Yes, but that's what the questioner wants. I'm not sure what you mean by "only makes sense with search()". It works perfectly fine with match.
  • CK5
    CK5 almost 2 years
    best answer among other answers
  • Montana Burr
    Montana Burr almost 2 years
    To be clear: You probably want to check if pattern.match returns something; luckily "None" is truthy, so you can just do "if pattern.match:"