gagsl-py at yahoo.com.ar
Sat Jan 20 07:13:27 CET 2007
<eight02645999 at yahoo.com> escribió en el mensaje
news:1169268024.164643.71320 at l53g2000cwa.googlegroups.com...
> suppose i have a string like
> how can i construct the regexp to get test3*test4*test5 and
> test7*test8, ie, i want to match * and the words before and after?
I suppose this is just an example and you mean "any word" instead of test1,
So your pattern would be: word*word*word*word, that is, word* repeated many
times, followed by another word.
To match a word we'll use "\w+", to match an * we have to use "\*" (it's a
So the regexp would be: "(\w+\*)+\w+"
Since we are not interested in the () as a group by itself -it was just to
describe the repeating pattern- we change it into a non-grouping
Final version: "(?:\w+\*)+\w+"
rexp = re.compile(r"(?:\w+\*)+\w+")
lines = [
for line in lines:
for txt in rexp.findall(line):
print '->', txt
Test it with some corner cases and see if it does what you expect: no "*",
starting with "*", ending with "*", embedded whitespace before and after the
"*", whitespace inside a word, the very definition of "word"...
More information about the Python-list