RegEx: find all occurances of a single character in a string

  • Thread starter Franz Steinhaeusler
  • Start date
F

Franz Steinhaeusler

given a string:

st="abcdatraataza"
^ ^ ^ ^ (these should be found)
I want to get the positions of all single 'a' characters.
(Without another 'a' neighbour)

So I tried:
r=re.compile('[^a]a([^a]')

but this applies only for
the a's, which has neighbours.
So I need also '^a' and 'a$'.

Am I doing something wrong?
Is there a easier solution?
How can I quickly get all these positions?

Thank you in advance.
 
F

Fredrik Lundh

Franz said:
given a string:

st="abcdatraataza"
^ ^ ^ ^ (these should be found)
I want to get the positions of all single 'a' characters.

for m in re.finditer("a+", st):
if len(m.group()) == 1:
print m.start()

or, perhaps:

indexes = [m.start() for m in re.finditer("a+", st) if len(m.group()) == 1]

</F>
 
F

Franz Steinhaeusler

Franz said:
given a string:

st="abcdatraataza"
^ ^ ^ ^ (these should be found)
I want to get the positions of all single 'a' characters.

for m in re.finditer("a+", st):
if len(m.group()) == 1:
print m.start()

or, perhaps:

indexes = [m.start() for m in re.finditer("a+", st) if len(m.group()) == 1]

</F>

Great!

thank you for your quick and helpful reply!
 
P

P

Franz said:
given a string:

st="abcdatraataza"
^ ^ ^ ^ (these should be found)
I want to get the positions of all single 'a' characters.
(Without another 'a' neighbour)

So I tried:
r=re.compile('[^a]a([^a]')

but this applies only for
the a's, which has neighbours.
So I need also '^a' and 'a$'.

Am I doing something wrong?
Is there a easier solution?
How can I quickly get all these positions?

Thank you in advance.

import re
s='abcdatraataza'
r=re.compile('(?<!a)a(?!a)')
a_list=[ match.start() for match in re2.finditer(s) ]
 
S

Steven Bethard

Franz said:
given a string:

st="abcdatraataza"
^ ^ ^ ^ (these should be found)
I want to get the positions of all single 'a' characters.
(Without another 'a' neighbour)

You could also try negative lookahead/lookbehind assertions:
.... print m.start()
....
0
4
10
12

Steve
 
P

P

import re
s='abcdatraataza'
r=re.compile('(?<!a)a(?!a)')
a_list=[ match.start() for match in re2.finditer(s) ]

Oops, tested this time:
----------------------------

import re


def index_letters(s,l):
regexp='(?<!%s)%s(?!%s)' % (l,l,l)
r=re.compile(regexp)
return [ match.start() for match in r.finditer(s) ]


print index_letters('abcdatraataza','a')
 
P

P

import re
s='abcdatraataza'
r=re.compile('(?<!a)a(?!a)')
a_list=[ match.start() for match in re2.finditer(s) ]

Oops, tested this time:
----------------------------

import re


def index_letters(s,l):
regexp='(?<!%s)%s(?!%s)' % (l,l,l)
r=re.compile(regexp)
return [ match.start() for match in r.finditer(s) ]


print index_letters('abcdatraataza','a')

Just comparing Fredrik Lundh's method:

r=re.compile("a+")
[m.start() for m in r.finditer(s) if len(m.group()) == 1]

Mine runs 100K iterations of 'abcdatraataza','a' in 1.4s
whereas Fredrik's does the same in 1.9s
 
F

Franz Steinhaeusler

given a string:

st="abcdatraataza"
^ ^ ^ ^ (these should be found)
I want to get the positions of all single 'a' characters.
(Without another 'a' neighbour)
[...]

Thank you again, Pádraig and Steve,
your solution looks still more compact.

There are several regular expression, that this could
almost justify an own newsgroup.

Or is there another group, where these questions are more
appropriate?
 
F

Fredrik Lundh

r=re.compile("a+")
[m.start() for m in r.finditer(s) if len(m.group()) == 1]
< Mine runs 100K iterations of 'abcdatraataza','a' in 1.4s
whereas Fredrik's does the same in 1.9s

sure, but how long did it take you to come up with a working RE? and how
many casual RE users will understand what the heck that RE is doing?

(insert obligatory remark about premature optimization, etc).

</F>
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top