Python's RE

Matthias Huening matthias.huening at univie.ac.at
Tue Jun 6 02:27:48 EDT 2000


Jerome Quelin <jerome.quelin at insalien.org> schrieb in im Newsbeitrag:
960229654.1963595132 at news.libertysurf.fr...
> Evgeny Zemlerub <oblom at barak-online.net> wrote:
> >How exectly do I something that reassembles perl's :
> >$string = "qwerwe kkljlksdfg (first string) asdkjflkjg
> >(secondstring)jlskjlfdkg(one more string)";
> >@words = ($string =~ /\((.+?)\)/smg);
> >foreach(@words){
> >        print "$_\n";
> >}
> Python does not support non-greedy quantifiers (ie, *?, +? and {,}?)...
> In fact, Perl is the only language that does, I think. You are to do it
> another way in Python.

You are wrong. From the library reference:

*?, +?, ??
The "*", "+", and "?" qualifiers are all greedy; they match as much text as
possible. Sometimes this behaviour isn't desired; if the RE <.*> is matched
against '<H1>title</H1>', it will match the entire string, and not just
'<H1>'. Adding "?" after the qualifier makes it perform the match in
non-greedy or minimal fashion; as few characters as possible will be
matched. Using .*? in the previous expression will match only '<H1>'.

Matthias







More information about the Python-list mailing list