J
John Salerno
Ben said:Yes, and no extra for loops are needed! You can define groups inside
the lookahead assertion:
['aba', 'aba', 'aba', 'aba', 'aba', 'aba', 'aba']
Wow, that was like magic!
Ben said:Yes, and no extra for loops are needed! You can define groups inside
the lookahead assertion:
['aba', 'aba', 'aba', 'aba', 'aba', 'aba', 'aba']
rg = r'(\w)(?=(.)\1)'
That would at least isolate the number, although you'd still have to get
it out of the list/tuple.
Mirco said:I have no idea how to do this
in Python in a terse way - but
I'll try ;-)
In Perl, its easy. Here, the
"match construct" (\w)(?=(.)\1)
returns all captures in a
list (a 1 a 2 a 4 b 7 c 9)
Ah, I see the difference. In Python you get a list of tuples, so there
seems to be a little extra work to do to get the number out.
> Ah, I see the difference. In Python you get a list of
> tuples, so there seems to be a little extra work to do
> to get the number out.
Mirco said:In Python, you have to deconstruct
the 2D-lists (here: long list of
short lists [a,2] ...) by
'slicing the slice':
char,num = list[:][:]
in a loop and using the apropriate element then:
import re
t = 'a1a2a3Aa4a35a6b7b8c9c';
r = r'(\w)(?=(.)\1)'
l = re.findall(r, t)
for a,b in (l[:][:]) : print b
In the moment, I find this syntax
awkward and arbitary, but my mind
should change if I'm adopted more
to this in the end ;-)
[this] is best done by a list comprehension:
l = [m[1] for m in re.findall(r, t)]
or, [...] a generator expression:
g = (m[1] for m in re.findall(r, t))
or
process(m[1] for m in re.findall(r, t))
... avoid creating the tuples, ... finditer instead:
l = [m.group(2) for m in re.finditer(r, t)]
g = (m.group(2) for m in re.finditer(r, t))
finditer is also a good tool to use
for m in re.finditer(r, t):
s = m.group(2)
... process s in some way ...
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.