Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
正規表現を使用して繰り返される文字を見つけるにはどうすればよいですか?
がある場合はaaabbab、3 回の繰り返しがある文字のみを一致させたいと思います。
aaabbab
aaa
を試してみてください。文字列はstring.scan(/((.)\2{2,})/).map(&:first)どこにありますか。string
string.scan(/((.)\2{2,})/).map(&:first)
string
これが機能する方法は、任意の文字を探してキャプチャし (ドット)、その文字の繰り返し (\2後方参照) を 2 回以上照合することです ({2,}範囲は「2 回から無限回の間の任意の場所」を意味します)。Scan は配列の配列を返すため、最初に一致したものをマップして目的の結果を取得します。
\2
{2,}