检查字符串是否与python中的正则表达式完全匹配

时间:2022-09-28 14:33:18

I want to check if a string completely matches a regex in python 3.

我想检查字符串是否与python 3中的正则表达式完全匹配。

For example, if I had :

例如,如果我有:

regex = "(A*B)*"
stringone = "AABAABB"
stringtwo = "ABCAAB"
  • stringone would be a match for regex,
  • stringone将匹配正则表达式,

  • stringtwo would not be a match.
  • stringtwo不会匹配。

I tried using the built-in re module but I kept getting none type objects no matter what I did.

我尝试使用内置的re模块,但无论我做什么,我都不会得到任何类型的对象。

This is my code

这是我的代码

compiled_regex = re.compile(regex)
if compiled_regex.match(string).group(0) == string:
    print("Match")

2 个解决方案

#1


You can add start of line (^) and end of line ($) patterns to your regex pattern:

您可以在行正则表达式模式中添加行开头(^)和行尾($)模式:

regex = r"^(A*B)*$"

pattern = re.compile(regex)
for s in "AABAABB", "ABCAAB", "B", "BBB", "AABAABBx", "xAABAABB":
    if pattern.match(s):
        print "Matched:", s

This outputs:

Matched: AABAABB
Matched: B
Matched: BBB

Or you can use your regex with the match object too, but use group(), not groups():

或者您也可以将正则表达式与匹配对象一起使用,但使用group(),而不是groups():

pattern = re.compile(r'(A*B)*')
for s in "AABAABB", "ABCAAB", "B", "BBB", "AABAABBx", "xAABAABB":
    m = pattern.match(s)
    if m is not None and m.group() == s:
        print "Matched:", s

Outputs:

Matched: AABAABB
Matched: B
Matched: BBB

#2


Your regular expression works fine, look:

你的正则表达式工作正常,看:

import re

regex = "(A*B)*"
stringone = "AABAABB"
stringtwo = "ABCAAB"

compiled_regex = re.compile(regex)
if compiled_regex.match(stringone):
    print("Match")
    print(compiled_regex.match(stringone))

Outputs:

Match                                                                                                                                                                                                                                                                                                        
<_sre.SRE_Match object; span=(0, 7), match='AABAABB'> 

If you want to additionally check that the string does not contain anything besides what the regex specifies you should use the ^ and $, as so:

如果你想另外检查字符串除了正则表达式指定的内容之外还不包含任何内容,你应该使用^和$,如下所示:

regex = "^(A*B)*$"

#1


You can add start of line (^) and end of line ($) patterns to your regex pattern:

您可以在行正则表达式模式中添加行开头(^)和行尾($)模式:

regex = r"^(A*B)*$"

pattern = re.compile(regex)
for s in "AABAABB", "ABCAAB", "B", "BBB", "AABAABBx", "xAABAABB":
    if pattern.match(s):
        print "Matched:", s

This outputs:

Matched: AABAABB
Matched: B
Matched: BBB

Or you can use your regex with the match object too, but use group(), not groups():

或者您也可以将正则表达式与匹配对象一起使用,但使用group(),而不是groups():

pattern = re.compile(r'(A*B)*')
for s in "AABAABB", "ABCAAB", "B", "BBB", "AABAABBx", "xAABAABB":
    m = pattern.match(s)
    if m is not None and m.group() == s:
        print "Matched:", s

Outputs:

Matched: AABAABB
Matched: B
Matched: BBB

#2


Your regular expression works fine, look:

你的正则表达式工作正常,看:

import re

regex = "(A*B)*"
stringone = "AABAABB"
stringtwo = "ABCAAB"

compiled_regex = re.compile(regex)
if compiled_regex.match(stringone):
    print("Match")
    print(compiled_regex.match(stringone))

Outputs:

Match                                                                                                                                                                                                                                                                                                        
<_sre.SRE_Match object; span=(0, 7), match='AABAABB'> 

If you want to additionally check that the string does not contain anything besides what the regex specifies you should use the ^ and $, as so:

如果你想另外检查字符串除了正则表达式指定的内容之外还不包含任何内容,你应该使用^和$,如下所示:

regex = "^(A*B)*$"