以下の文字列で 0123 のすべての順列を検索したい
文字列内の 0123 マッチングの順列を与えることができる正規表現を使用できますか? また、重複するパターンがある場合は必要です
「0123」ここでは [1023][1230][2301][3012] の一致が必要です
以下の文字列で 0123 のすべての順列を検索したい
文字列内の 0123 マッチングの順列を与えることができる正規表現を使用できますか? また、重複するパターンがある場合は必要です
「0123」ここでは [1023][1230][2301][3012] の一致が必要です
正規表現ではありませんが、C++11:
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
const std::string s("01210210021212333212300213231102023103130001332121230221000012333333021032112");
const std::string ref("0123");
if(ref.length() > s.length())
{
return 0;
}
for(int i = 0; i < s.length() - ref.length(); ++i)
{
if(std::is_permutation(s.cbegin()+i, s.cbegin()+i+ref.length(), ref.cbegin()))
{
const std::string extract(s, i, ref.length());
std::cout << extract << std::endl;
}
}
return 0;
}
たとえば、次のようにコンパイルしますg++ -std=c++11 -o sample sample.cpp
絶対に正規表現が必要な場合:(?=[0123]{3})(.)(?!\1)(.)(?!\1|\2)(.)(?!\1|\2|\3).
つまり:
(?=[0123]{3}) : positive assertion that the 4 next characters are 0, 1, 2, 3
(.) : capture first character
(?!\1) : assert that following character is not the first capture group
(.) : capture second character
(?!\1|\2) : assert that following character is neither the first nor the second capture group
etc.