如何检查字符串是否与此模式匹配?

大写字母,数字,大写字母,数字…

例如,这些将匹配:

A1B2
B10L1
C1N200J1

这些不会('^'指向问题)

a1B2
^
A10B
   ^
AB400
^

当前回答

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

其他回答

re.match(r"pattern", string) #不需要编译

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

如果需要,可以将其计算为bool值

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

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

我相信这对于大写的数字模式是适用的。

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

请尝试以下方法:

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())
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'