188

Using Python regular expressions how can you get a True/False returned? All Python returns is:

<_sre.SRE_Match object at ...>
1
  • 1
    try bool(re.search(pattern=META_VAR_REGEX, string=coq_str)). Commented Jul 21, 2022 at 14:45

6 Answers 6

264

If you really need True or False, just use bool

>>> bool(re.search("hi", "abcdefghijkl"))
True
>>> bool(re.search("hi", "abcdefgijkl"))
False

As other answers have pointed out, if you are just using it as a condition for an if or while, you can use it directly without wrapping in bool()

Sign up to request clarification or add additional context in comments.

6 Comments

bool value is needed when the conditional statement contains boolean arithmetic operation. eg: if (re.search("a","abc") & True):
@AizzatSuhardi, & is a bitwise operaration. and would be the boolean operation.
thanks @JohnLaRooy. correction: (re.search("a","abc") and True)
Wrapping in bool makes the intention of the programmer clear to the reader.
This is not just clearer, in some cases necessary i.e. if you are using the assertTrue function in python unit test to check a regex match (you need a boolean value, not expression). Or any function where you need a boolean input...
|
187

Match objects are always true, and None is returned if there is no match. Just test for trueness.

if re.match(...):

8 Comments

re.match(...) would return true if the string's beginning part match the regular pattern. While search will confirm the pattern anywhere in the string.
It may be more desirable to use if re.match(...) is None: instead
sorry, did you address the comments in your answer? Its unclear to me, do you mind clarifying?
May I ask why re is designed like this? If match objects are always true, why doesn't it just return True at the first place, given that we always need to know whether the answer is true or false anyway?
@ytu: Because then you can do everything else you need to.
|
16

Here is my method:

import re
# Compile
p = re.compile(r'hi')
# Match and print
print bool(p.match("abcdefghijkl"))

3 Comments

p = re.compile(r'hi') :-P
yeah and it returns "False"
if you change match to search, it'll return True
12

Ignacio Vazquez-Abrams is correct. But to elaborate, re.match() will return either None, which evaluates to False, or a match object, which will always be True as he said. Only if you want information about the part(s) that matched your regular expression do you need to check out the contents of the match object.

Comments

9

One way to do this is just to test against the return value. Because you're getting <_sre.SRE_Match object at ...> it means that this will evaluate to true. When the regular expression isn't matched you'll the return value None, which evaluates to false.

import re

if re.search("c", "abcdef"):
    print "hi"

Produces hi as output.

2 Comments

You always get a return value; None is the default if nothing is explicitly returned.
Thanks -- corrected that. I was just going by what I saw in the REPL.
4

You can use re.match() or re.search(). Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default). refer this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.