简体   繁体   English

检查字符串是否匹配模式

[英]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
^
import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.search(string)

One-liner: re.match(r"pattern", string) # No need to compile 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

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

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

import re

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


I believe that should work for an uppercase, number pattern.我相信这应该适用于大写的数字模式。

As stated in the comments, all these answers using re.match implicitly matches on the start of the string.如评论中所述,所有这些使用re.match的答案都隐式匹配字符串的开头。 re.search is needed if you want to generalize to the whole string.如果您想推广到整个字符串,则需要re.search

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.信用:@LondonRob 和 @conradkleinespel 在评论中。

regular expressions make this easy ...正则表达式使这变得容易......

[AZ] will match exactly one character between A and Z [AZ]将匹配 A 和 Z 之间的一个字符

\\d+ will match one or more digits \\d+将匹配一位或多位数字

() group things (and also return things... but for now just think of them grouping) ()事物分组(并且还返回事物......但现在只考虑将它们分组)

+ selects 1 or more +选择 1 个或多个

Careful!小心! (Maybe you want to check if FULL string matches) (也许你想检查 FULL 字符串是否匹配)

The re.match(...) will not work if you want to match the full string.如果要匹配完整的字符串, re.match(...)将不起作用。

For example;例如;

  • re.match("[az]+", "abcdef") ✅ will give a match re.match("[az]+", "abcdef") ✅ 将给出匹配
  • But!但! re.match("[az]+", "abcdef 12345") ✅ will also give a match because there is a part in string which matches (maybe you don't want that when you're checking if the entire string is valid or not) re.match("[az]+", "abcdef 12345") ✅ 也会给出匹配,因为字符串中有一部分匹配(当您检查整个字符串是否有效时,您可能不希望这样或不)

Solution解决方案

Use re.fullmatch(...) .使用re.fullmatch(...) This will only match if the这将只匹配,如果

if re.fullmatch("[a-z]+", my_string):
    print("Yes")
Example例子
  • re.fullmatch("[az]+", "abcdef") ✅ Yes re.fullmatch("[az]+", "abcdef") ✅ 是
  • re.fullmatch("[az]+", "abcdef 12345") ❌ No re.fullmatch("[az]+", "abcdef 12345") ❌ 否

One liner: bool(re.fullmatch("[az]+", my_string))一个班轮: bool(re.fullmatch("[az]+", my_string))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM