6 Common Problems 6.1 Use String Methods 6.2 match() versus search() 17 Adding$_{.*}$ defeats this optimization, requiring scanning to the end of the string and then backtracking to find a match for the rest of the RE. Use$_{re.search()}$ instead. Sometimes you’ll be tempted to keep using$_{re.match()}$ , and just add$_{.*}$ to the front of your RE. Resist this temptation and use$_{re.search()}$ instead. The regular expression compiler does some analysis of REs in order to speed up the process of looking for a match. One such analysis figures out what the first character of a match must be; for example, a pattern starting with$_{Crow}$ must match starting with a$_{'C'}$ . The analysis lets the engine quickly scan through the string looking for the starting character, only trying the full match if a$_{'C'}$ is found. The$_{match()}$ function only checks if the RE matches at the beginning of the string while$_{search()}$ will scan forward through the string for a match. It’s important to keep this distinction in mind. Remember,$_{match()}$ will only report a successful match which will start at 0; if the match wouldn’t start at zero,$_{match()}$ will not report it. On the other hand,$_{search()}$ will scan forward through the string, reporting the first match it finds. >>> print(re.search('super','superstition').span()) (0, 5) >>> print(re.search('super','insuperable').span()) (2, 7) >>> print(re.match('super','superstition').span()) (0, 5) >>> print(re.match('super','insuperable')) None In short, before turning to the$_{re}$ module, consider whether your problem can be solved with a faster and simpler string method. Another common task is deleting every occurrence of a single character from a string or replacing it with another single character. You might do this with something like$_{re.sub('
', ' ', S)}$ , but$_{translate()}$ is capable of doing both tasks and will be faster than any regular expression operation can be. One example might be replacing a single fixed string with another one; for example, you might replace word with $_{deed}$. re.sub() seems like the function to use for this, but consider the replace() method. Note that$_{replace()}$ will also replace$_{word}$ inside words, turning$_{swordfish}$ into$_{sdeedfish}$ , but the naive RE word would have done that, too. (To avoid performing the substitution on parts of words, the pattern would have to be \bword\b , in order to require that word have a word boundary on either side. This takes the job beyond replace() ’s abilities.) Sometimes using the$_{re}$ module is a mistake. If you’re matching a fixed string, or a single character class, and you’re not using any$_{re}$ features such as the$_{IGNORECASE}$ flag, then the full power of regular expressions may not be required. Strings have several methods for performing operations with fixed strings and they’re usually much faster, because the implementation is a single small C loop that’s been optimized for the purpose, instead of the large, more generalized regular expression engine. Regular expressions are a powerful tool for some applications, but in some ways their behaviour isn’t intuitive and at times they don’t behave the way you may expect them to. This section will point out some of the most common pitfalls.