By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Regex for repeated character?

P: n/a
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']
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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']

Peter

Jul 19 '05 #2

P: n/a
Peter Otten wrote:
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
Jul 19 '05 #3

P: n/a
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

Jul 19 '05 #4

P: n/a
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

Jul 19 '05 #5

P: n/a
>>>>> "Leif" == Leif K-Brooks <eu*****@ecritters.biz> writes:

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
Jul 19 '05 #6

P: n/a
In article <5J*****************@newshog.newsread.com>,
Leif K-Brooks <eu*****@ecritters.biz> 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']

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

['a', 'bb', 'ccc', 'dddd', 'ccc', 'bb', 'a']

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.
Jul 19 '05 #7

P: n/a
Doug Schwarz wrote:
In article <5J*****************@newshog.newsread.com>,
Leif K-Brooks <eu*****@ecritters.biz> 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']


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 :-)
Jul 19 '05 #8

P: n/a
On Saturday 18 June 2005 02:05 am, John Machin wrote:
Doug Schwarz wrote:
In article <5J*****************@newshog.newsread.com>,
Leif K-Brooks <eu*****@ecritters.biz> wrote:
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. ;-)

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #9

P: n/a
Terry Hancock wrote:
On Saturday 18 June 2005 02:05 am, John Machin wrote:
Doug Schwarz wrote:
In article <5J*****************@newshog.newsread.com>,
Leif K-Brooks <eu*****@ecritters.biz> wrote:

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)

Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.