175

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

<_sre.SRE_Match object at ...>
Xantium
  • 11,201
  • 10
  • 62
  • 89
nobody
  • 1,859
  • 2
  • 12
  • 5

6 Answers6

243

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

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
178

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

if re.match(...):
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 30
    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. – Yu Shen Jun 08 '13 at 09:57
  • 10
    It may be more desirable to use ``if re.match(...) is None:`` instead – NuclearPeon Feb 01 '17 at 22:42
  • 2
    sorry, did you address the comments in your answer? Its unclear to me, do you mind clarifying? – Charlie Parker Feb 08 '17 at 03:20
  • 2
    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 May 24 '18 at 03:33
  • 2
    @ytu: Because then you can do [everything else you need to](https://docs.python.org/3/library/re.html#match-objects). – Ignacio Vazquez-Abrams May 24 '18 at 03:34
  • 1
    Note that `<_sre.SRE_Match... == True` and `<_sre.SRE_Match... == False` are both `False`, so you may wish to cast the result to a `bool` in some cases. – Pikamander2 Oct 03 '19 at 03:41
  • @NuclearPeon `re.match` will never return `False`, so is that actually useful? Or is it to cover situations where you're not sure what value might be passed in? – wjandrea Dec 27 '19 at 01:39
  • 1
    Why not just convert to bool `if bool(re.match(...)):` – Shakeel Apr 18 '20 at 21:22
15

Here is my method:

import re
# Compile
p = re.compile(r'hi')
# Match and print
print bool(p.match("abcdefghijkl"))
Héctor Valverde
  • 1,089
  • 1
  • 14
  • 34
Vaibhav Desai
  • 2,334
  • 2
  • 25
  • 29
11

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.

Xantium
  • 11,201
  • 10
  • 62
  • 89
cory
  • 341
  • 3
  • 15
8

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.

Gavin Anderegg
  • 6,281
  • 2
  • 25
  • 35
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

Sashini Hettiarachchi
  • 1,630
  • 2
  • 10
  • 21