使用以下正则表达式匹配该模式:
^[ABC]-[69]\d+$
这个正则表达式可以匹配"A-6", "B-9", 和"C-6",但是不能匹配"A-5" 或者 "D-9" 。这是因为它只接受字母A、B、C,紧接着一个连字符,6或9,然后后面的数字可以是任意多个。
可以使用以下 Python 代码进行测试:
import re
# 匹配该模式的测试字符串列表
test_strings = ["A-6000", "B-9", "C-678", "A-550", "D-9"]
# 匹配正则表达式的函数
def match_pattern(pattern, test_string):
return re.match(pattern, test_string) is not None
# 匹配正则表达式
for test_string in test_strings:
if match_pattern("^[ABC]-[69]\d+$", test_string):
print(f"{test_string} 是匹配的")
else:
print(f"{test_string} 不匹配")
输出结果为:
A-6000 是匹配的
B-9 是匹配的
C-678 是匹配的
A-550 不匹配
D-9 不匹配