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

Albert-Jan Roskam sjeik_appie at hotmail.com
Mon Jan 18 10:12:08 EST 2016


 
> From: dyoo at hashcollision.org
> Date: Mon, 18 Jan 2016 00:29:41 -0800
> Subject: Re: [Tutor] str.strip strange result...?
> To: sjeik_appie at hotmail.com
> CC: __peter__ at web.de; tutor at python.org
> 
> > 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. Yes, certainly: https://xkcd.com/208/ :-)
> 
> 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.
Agreed. Apart from backslashes, re.escape could also be really handy. But regexes are scary and intimidating for those who have never seen them before.
> 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.
re.compile helps, though it always bothers me that I "need" to put them as a global in the top-level of my module (along with collections.namedtuple).Unless I use a class, where __init__ is a good place.
 
> 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.
 One other downside is that it takes only a couple of days/beers before one forgets the regex. But re.VERBOSE and named groups help a LOT.
 
> Good luck to you!
 		 	   		  


More information about the Tutor mailing list