[Tutor] str.strip strange result...?

Danny Yoo dyoo at hashcollision.org
Mon Jan 18 03:29:41 EST 2016


> Not sure which one is faster, but in this case I actually find a regex more readable (!):
>>>> re.sub(r"_1$", "", "V01_1")
> 'V01'


Hi Albert-Jan Roskam,

Here's a minor counterpoint to using regexes here: they're sometimes a
bit too powerful.

In this situation, a regex approach might be troublesome if parts of
the suffix can be interpreted as regular expression meta-characters.
For example, if we were trying to strip out a suffix like

    ".exe"

then if we were to try to do this with regexes, we might forget that
"." is a meta-character that acts as a wildcard for any single
character.  Therefore, a regexp-based solution for the general
suffix-removal problem is complicated because we'd need consider
escaping certain characters.  Not that this would be hard, but that
it's yet another detail we have to keep in our heads.

Another disadvantage is that, if the suffix is dynamically determined,
then there's an additional cost in "compiling" the regular expression:
building the pattern-matching machinery doesn't come for free.


For those reasons, I think the regexp approach here is a little bit of
overkill.  This kind of tradeoff is the sort of thing that reference
documentation will probably not mention.   That's why this list is
here, to share the experience of using these systems with other
beginners and learners.  Regexes are still very useful: just be aware
that they have sharp edges.


Good luck to you!


More information about the Tutor mailing list