LoginSignup
7
4

More than 5 years have passed since last update.

同じ文字が N 個以上連続する部分文字列を見つける正規表現

Last updated at Posted at 2012-12-26

同じ文字が N 個以上連続する部分文字列を見つける正規表現です。

import re


def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]


print(list(nchars('a いい uuu ee お', 2)))
print(list(nchars('aa いいい ううううう e ooo', 3)))

実行結果です。

['いい', 'uuu', 'ee']
['いいい', 'ううううう', 'ooo']
7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4