Regex for repeated character?

L

Leif K-Brooks

How do I make a regular expression which will match the same character
repeated one or more times, instead of matching repetitions of any
(possibly non-same) characters like ".+" does? In other words, I want a
pattern like this:
>>> re.findall(".+", "foo") # not what I want ['foo']
>>> re.findall("something", "foo") # what I want
['f', 'oo']
 
P

Peter Otten

Leif said:
How do I make a regular expression which will match the same character
repeated one or more times, instead of matching repetitions of any
(possibly non-same) characters like ".+" does? In other words, I want a
pattern like this:
re.findall(".+", "foo") # not what I want ['foo']
re.findall("something", "foo") # what I want
['f', 'oo']

This is as close as I can get:
[m.group() for m in re.compile(r"(.)(\1*)").finditer("foo bar baaaz")]
['f', 'oo', ' ', 'b', 'a', 'r', ' ', 'b', 'aaa', 'z']

Peter
 
J

John Machin

Peter said:
Leif K-Brooks wrote:

How do I make a regular expression which will match the same character
repeated one or more times, instead of matching repetitions of any
(possibly non-same) characters like ".+" does? In other words, I want a
pattern like this:
re.findall(".+", "foo") # not what I want ['foo']
re.findall("something", "foo") # what I want
['f', 'oo']


This is as close as I can get:

[m.group() for m in re.compile(r"(.)(\1*)").finditer("foo bar baaaz")]

['f', 'oo', ' ', 'b', 'a', 'r', ' ', 'b', 'aaa', 'z']

Another way, ever so slightly less complicated:
>>> [x[0] for x in re.findall(r"((.)\2*)", "foo bar baaaz")]
['f', 'oo', ' ', 'b', 'a', 'r', ' ', 'b', 'aaa', 'z']

Cheers,
John
 
P

Paul McGuire

A brute-force pyparsing approach - define an alternation of all
possible Words made up of the same letter.
Plus an alternate version that just picks out the repeats, and gives
their location in the input string:

from pyparsing import ZeroOrMore, MatchFirst, Word, alphas

print "group string by character repeats"
repeats = ZeroOrMore( MatchFirst( [ Word(a) for a in alphas ] ) )
test = "foo ooobaaazZZ"
print repeats.parseString(test)
print

print "find just the repeated characters"
repeats = MatchFirst( [ Word(a,min=2) for a in alphas ] )
test = "foo ooobaaazZZ"
for toks,loc,endloc in repeats.scanString(test):
print toks,loc

Gives:
group string by character repeats
['f', 'oo', 'ooo', 'b', 'aaa', 'z', 'ZZ']

find just the repeated characters
['oo'] 1
['ooo'] 4
['aaa'] 8
['ZZ'] 12

(pyparsing implicitly ignores whitespace, that's why there is no ' '
entry in the first list)

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul
 
P

Paul McGuire

One more bit, add this on to the code in the previous post:

print "collapse repeated characters"
repeats.setParseAction(lambda s,l,toks: toks[0][0])
print test,"->",repeats.transformString(test)

Gives:

collapse repeated characters
foo ooobaaazZZ -> fo obazZ
 
C

Chris Smith

Leif> How do I make a regular expression which will match the same
Leif> character repeated one or more times, instead of matching
Leif> repetitions of any (possibly non-same) characters like ".+"
Leif> does? In other words, I want a pattern like this:
>>>> re.findall(".+", "foo") # not what I want Leif> ['foo']
>>>> re.findall("something", "foo") # what I want
Leif> ['f', 'oo']

Do you mean:


http://www.python.org/doc/current/lib/re-syntax.html


{m}
Specifies that exactly m copies of the previous RE should be
matched; fewer matches cause the entire RE not to match. For
example, a{6} will match exactly six "a" characters, but not five.

{m,n}
Causes the resulting RE to match from m to n repetitions of the
preceding RE, attempting to match as many repetitions as
possible. For example, a{3,5} will match from 3 to 5 "a"
characters. Omitting m specifies a lower bound of zero, and
omitting n specifies an infinite upper bound. As an example,
a{4,}b will match aaaab or a thousand "a" characters followed by a
b, but not aaab. The comma may not be omitted or the modifier
would be confused with the previously described form.

{m,n}?
Causes the resulting RE to match from m to n repetitions of the
preceding RE, attempting to match as few repetitions as
possible. This is the non-greedy version of the previous
qualifier. For example, on the 6-character string 'aaaaaa', a{3,5}
will match 5 "a" characters, while a{3,5}? will only match 3
characters.


HTH,
Chris
 
D

Doug Schwarz

Leif K-Brooks said:
How do I make a regular expression which will match the same character
repeated one or more times, instead of matching repetitions of any
(possibly non-same) characters like ".+" does? In other words, I want a
pattern like this:
re.findall(".+", "foo") # not what I want ['foo']
re.findall("something", "foo") # what I want
['f', 'oo']


How's this?
>>> [x[0] for x in re.findall(r'((.)\2*)', 'abbcccddddcccbba')]
['a', 'bb', 'ccc', 'dddd', 'ccc', 'bb', 'a']
 
J

John Machin

Doug said:
How do I make a regular expression which will match the same character
repeated one or more times, instead of matching repetitions of any
(possibly non-same) characters like ".+" does? In other words, I want a
pattern like this:
re.findall(".+", "foo") # not what I want ['foo']
re.findall("something", "foo") # what I want
['f', 'oo']



How's this?
[x[0] for x in re.findall(r'((.)\2*)', 'abbcccddddcccbba')]
['a', 'bb', 'ccc', 'dddd', 'ccc', 'bb', 'a']

I think it's fantastic, but I'd be bound to say that given that it's the
same as what I posted almost two days ago :)
 
T

Terry Hancock

Doug said:
How do I make a regular expression which will match the same character
repeated one or more times,
How's this?
[x[0] for x in re.findall(r'((.)\2*)', 'abbcccddddcccbba')]
['a', 'bb', 'ccc', 'dddd', 'ccc', 'bb', 'a']

I think it's fantastic, but I'd be bound to say that given that it's the
same as what I posted almost two days ago :)

Guess there's only one obvious way to do it, then. ;-)
 
J

John Machin

Terry said:
Doug said:
How do I make a regular expression which will match the same character
repeated one or more times,

How's this?

[x[0] for x in re.findall(r'((.)\2*)', 'abbcccddddcccbba')]
['a', 'bb', 'ccc', 'dddd', 'ccc', 'bb', 'a']

I think it's fantastic, but I'd be bound to say that given that it's the
same as what I posted almost two days ago :)


Guess there's only one obvious way to do it, then. ;-)

Yep ... but probably a zillion ways in
re.compile(r"perl", re.I).match(other_languages)
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top