0

Below match doesn't work:

import re
pattern = re.compile("[\^\/!*\[({%?$]")

param = "f00.*"

if pattern.match(param):
    print " I am a regexp"
else:
    print "non regexp"

But this does:

import re
node_pattern = re.search("[\^\/!*\[({%?$]", "f00.*")

print bool(node_pattern)

What's wrong with re.compile()?

Isn't it the valid way to match a string against a regexp?

zubug55
  • 729
  • 7
  • 27

1 Answers1

1

Your question is not so much about re.compile() as it is about the difference between re.search() and re.match(), as Lev Zakharov pointed out. This code works the way you want it to, only changing match to search:

import re
pattern = re.compile("[\^\/!*\[({%?$]")

param = "f00.*"

if pattern.search(param):
    print " I am a regexp"
else:
    print "non regexp"

Maybe you have a more specific question about your use case that we are not getting.

dustintheglass
  • 197
  • 1
  • 11