Python's RE
Matthias Huening
matthias.huening at univie.ac.at
Mon Jun 5 10:50:58 EDT 2000
>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";
>}
I know there must be a (much) easier way to do this, but the following
code will do the job:
######
import string, re
the_string = r"""aaa(foo)bbb(bar)asdfsadf
ee(g)(adfsadf)ddd xxxx"""
elements = string.split(the_string, ')')
x = re.compile(r'\((.+)$', re.M | re.S)
for element in elements:
z = x.search(element)
if z: print z.group(1)
######
On the other hand, if you do it like this, you don't even need Regular
Expressions at all... The string module will be sufficient:
for element in elements:
z = string.find(element, "(")
if z != -1:
print element[z+1:]
Hope this helps,
Matthias
- - - - -
matthias.huening at univie.ac.at
http://www.ned.univie.ac.at/
More information about the Python-list
mailing list