ChatGPT解决这个技术问题 Extra ChatGPT

Check if string matches pattern

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
^
could you please explain more why it is a problem?
^([A-Z]\d+){1,}$ like this?
In your third example, the problem should be with B and not with A.
maybe it's a typo error on the problem. both A and B are small letters right? A10b and aB400?
@Burhan, The problem is with A because B has numbers next to it and A doesn't

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

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.
@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.
I edited your answer, because it only makes sense with search() in this context.
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.
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:"
n
nehem

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

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?
Careful with re.match. It only matches at the start of a string. Have a look at re.search instead.
@LondonRob probably because if checks for the match not being None.
@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.
@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.
E
Edd

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

This is the only case that returns the match which is required for getting groups. Best answer in my opinion.
best answer among other answers
M
Marc Cohen
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'

K
Kneel-Before-ZOD

import re

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

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


J
Joran Beasley

regular expressions make this easy ...

[A-Z] will match exactly one character between A and Z

\d+ will match one or more digits

() group things (and also return things... but for now just think of them grouping)

+ selects 1 or more


c
crypdick

As stated in the comments, all these answers using re.match implicitly matches on the start of the string. re.search is needed if you want to generalize to the whole string.

import re

pattern = re.compile("([A-Z][0-9]+)+")

# finds match anywhere in string
bool(re.search(pattern, 'aA1A1'))  # True

# matches on start of string, even though pattern does not have ^ constraint
bool(re.match(pattern, 'aA1A1'))  # False

Credit: @LondonRob and @conradkleinespel in the comments.