473,473 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regex for repeated character?

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
9 10538
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
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
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
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
>>>>> "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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
7
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim...
6
by: ipramod | last post by:
Hi Can anyone tell me the meaning of the following regex: ^.*1234 ^.*\n.*1234 ^.*1234.*\n.*1234 ^.*+.*1234 ^.*1234+
9
by: deko | last post by:
As I understand it, the characters that make up an Internet domain name can consist of only alpha-numeric characters and a hyphen (http://tools.ietf.org/html/rfc3696) So I'm trying to write...
5
by: TheSteph | last post by:
Hi, I'm new to Regex.. Could someone show me how I can extract substring enclosed in ? Example :
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
2
by: nuffnough | last post by:
I am doing a string.replace in a simple table generation app I wrote, and I can't figure out how to match whitespace with /s, so I thought I would see if osmeone where would be kind enough to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.