faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes

Entry

JPython-compatible re.findall

Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 347, Magnus L. Hetland


"""
Packages: jpython;text.regular_expressions
"""
"""
From:           	mlh@vier.idi.ntnu.no (Magnus L. Hetland)
Subject:        	Re: JPython - Problems using "re"
Date sent:      	08 Dec 2020 15:57:26 +0100
Organization:   	Norwegian university of science and technology
To:             	python-list@python.org
> But in JPython, (JPython 1.1 beta 4), "findall" isn't a method in the "re" 
> module. 
> Is there another way to produce the same result (scan a string and return a 
> list of all matches)?
Use a while-loop...
"""
import re
p = re.compile("(pattern)")
t = "this text contains the pattern several times (pattern pattern)"
pos = 0
result = []
m = p.search(t,pos)
while m:
    g = m.groups()
    if len(g) == 1:
        result.append(g[0])
    else:
        result.append(g)
    pos = m.end()
    m = p.search(t,pos)
print result