[issue29977] re.sub stalls forever on an unmatched non-greedy case

Gareth Rees report at bugs.python.org
Tue Apr 4 04:03:16 EDT 2017


Gareth Rees added the comment:

The problem here is that both "." and "\s" match a whitespace character, and because you have the re.DOTALL flag turned on this includes "\n", and so the number of different ways in which (.|\s)* can be matched against a string is exponential in the number of whitespace characters in the string.

It is best to design your regular expression so as to limit the number of different ways it can match. Here I recommend the expression:

    /\*(?:[^*]|\*[^/])*\*/

which can match in only one way.

----------
nosy: +gdr at garethrees.org

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29977>
_______________________________________


More information about the Python-bugs-list mailing list