Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 11
    re module compiles and caches regexes automagically, so there is no need to precompile Commented Jan 22, 2014 at 17:17
  • 2
    @wim: it caches the last X usages, so it's technically possible to use X+5 regexes and then do a natural sort over and over, at which point this wouldn't be cached. but probably negligible in the long run Commented Jan 22, 2014 at 17:51
  • 3
    The X usages mentioned by @Claudiu seem to be 100 on Python 2.7 and 512 on Python 3.4. And also note that when the limit is reached the cache is completely cleared (so it's not only the oldest one that is thrown out). Commented Nov 23, 2015 at 12:45
  • 5
    This doesn't work when the list elements are Path objects. You can modify your function to make it work though, just replace the last _nsre.split(s) with _nsre.split(str(s)). Same for the lambda, replace s with str(s) at the end of the expression. Commented Oct 23, 2019 at 13:57
  • 2
    Why did you use [0-9] in the function and \d in the lambda? As far as I know those are equal, but wouldnt it make sense to adjust the answer then to use only one of the two? Commented Sep 10, 2022 at 9:44