21

When you run re.match for the original string and the string that was passed through re.compile, how is the latter different? What happened to the string that was passed through re.compile?

mango
  • 543
  • 1
  • 5
  • 14
  • Look at the docs first... – Alberto Megía Dec 04 '13 at 21:30
  • 8
    this question is similar to this one: http://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile even though you are marked down (-1 vs 155 in link) I think it is a good question to ask. Stackoverflow python community has a habit of "down vote first understand question later" – Har May 28 '14 at 10:52

1 Answers1

8

It compiles a regex into a regex object. Take a look at the docs for more info.

JonathanV
  • 2,484
  • 1
  • 13
  • 9
  • 2
    re.match(pattern, searchstring) builds up a PatternObject internally from the pattern and then applies the searchstring to the PatternObject. If you are going to run the same pattern on multiple searchstrings, you should compile the pattern into a PatternObject to eliminate redundant work. Bottom-line: your code will perform the same but will run faster. – IceArdor Dec 04 '13 at 21:31
  • 4
    @IceArdor -- While you're technically correct, `re` actually caches a bunch of regex's in a dict -- So, if you don't compile them explicitly, `re` will compile the regex on the first call and use the compiled version on subsequent calls for efficiency. You'll only see a significant speedup if you program uses more than `re._MAXCACHE` regular expressions causing the cache to be cleared and rebuilt. I would argue that you should compile the regex's for code clarity, more than for speed. – mgilson Dec 04 '13 at 21:43
  • 1
    Ahh. Thanks for the clarification. My re._MAXCACHE=100, and I was using around 1000 patterns, so re's internal caching wasn't helping. *Off-topic: I always have issues choosing a variable name my compiled pattern object. Any suggestions?* – IceArdor Dec 04 '13 at 22:06