Regex for repeated character?

Chris Smith smitty_one_each at bigfoot.com
Fri Jun 17 19:24:26 EDT 2005


>>>>> "Leif" == Leif K-Brooks <eurleif at 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



More information about the Python-list mailing list