builtin regular expressions?
Mirco Wahab
wahab at chemie.uni-halle.de
Sat Sep 30 15:30:42 EDT 2006
Thus spoke MRAB (on 2006-09-30 20:54):
> Antoine De Groote wrote:
>> I just have to learn accept the fact that Python is more verbose more
>> often than Ruby (I don't know Perl really).
>
> One of the differences between the Python way and the Perl way is that
> the Perl way has a side-effect: Perl assigns to the variables $1, $2,
> etc. each time you execute a regular expression whereas Python just
> returns a match object for each, so it's not overwriting the results of
> the previous one. I find the Python way cleaner.
This statement reduces to the fact, that
- in Python you keep the matches if you want,
but drop them if you don't
import re
text = "this is 5 some 89 stuff 12 with numbers 21 interspersed"
matches = re.split('\D+', text)
- in Perl you keep the matches if you want,
but drop them if you don't
$text = "this is 5 some 89 stuff 12 with numbers 21 interspersed";
@matches = $text=~/(\d+)/g;
I fail to see the benefit of a re-object, I consider these to
be just there because regexes aren't in the core language.
Regards
Mirco
More information about the Python-list
mailing list