RegexReplace
-
QString QRegexReplace(coQString str, const QString& regex , const QString& replace, QRegularExpression::PatternOption flag) { QRegularExpression re(regex, flag); return str.replace(re, replace); } QString s = "111111111111111111111111111111111111111110"; s = QRegexReplace(s, ".{6}", "|$0");My function is replacing
sto|$0|$0|$0|$0|$0|$0|$0How i could get the same result as seen on: https://regex101.com/r/bsgCKf/1
Im trying to getsto|111111|111111|111111|111111|111111|111111|111110 -
QString QRegexReplace(coQString str, const QString& regex , const QString& replace, QRegularExpression::PatternOption flag) { QRegularExpression re(regex, flag); return str.replace(re, replace); } QString s = "111111111111111111111111111111111111111110"; s = QRegexReplace(s, ".{6}", "|$0");My function is replacing
sto|$0|$0|$0|$0|$0|$0|$0How i could get the same result as seen on: https://regex101.com/r/bsgCKf/1
Im trying to getsto|111111|111111|111111|111111|111111|111111|111110 -
@Cesar
I don't know, your way is not supported by the regular expressions used in Qt is all I can say. The one I gave you works in PCRE (PHP < 7.3), maybe that is what Qt uses.@JonB
From the Qt5/Qt6 doc:
„QRegularExpression implements Perl-compatible regular expressions.“ -
@JonB
From the Qt5/Qt6 doc:
„QRegularExpression implements Perl-compatible regular expressions.“@DerReisende
The problem is that is apparently/evidently not true, or not sufficiently accurate!On the regex site they offer "flavor" of PCRE2 (PHP >= 7.3) and their "Help" for that states "Perl PCRE2". And if you use
|$0for Substitution that gives the OP's desired result of|111111|111111|111111|111111|111111|111111|111110. You can see this from the link the OP supplied: https://regex101.com/r/bsgCKf/1That would be fine if OP then reported using that in Qt as
QRegexReplace(s, ".{6}", "|$0");delivered same. The problem is it does not! He says that gives him|$0|$0|$0|$0|$0|$0|$0, i.e.$0is not being recognised/substituted. That can be seen if e.g. you change "flavor" over to Python, which does not recognise$0.I found that did work in various "flavors" with parentheses-capture and
\1, see my https://regex101.com/r/YOrWq9/1, hence my suggestion OP tryQRegexReplace(s, "(.{6})", "|\\1");in Qt, which he confirms "This does work"....