Aki*_*iva 3 regex qt qregularexpression
我希望QString允许这样:
QString myString("School is LameCoolLame and LameRadLame");
myString.replace(QRegularExpression("Lame(.+?)Lame"),"\1");
Run Code Online (Sandbox Code Playgroud)
离开
"School is Cool and Rad"
Run Code Online (Sandbox Code Playgroud)
与我在文档中看到的不同,执行此操作需要您做更多的复杂(从文档中):
QRegularExpression re("\\d\\d \\w+");
QRegularExpressionMatch match = re.match("abc123 def");
if (match.hasMatch()) {
QString matched = match.captured(0); // matched == "23 def"
// ...
}
Run Code Online (Sandbox Code Playgroud)
或者在我的情况下这样的事情:
QString myString("School is LameCoolLame and LameRadLame");
QRegularExpression re("Lame(.+?)Lame");
QRegularExpressionMatch match = re.match(myString);
if (match.hasMatch()) {
for (int i = 0; i < myString.count(re); i++) {
QString newString(match.captured(i));
myString.replace(myString.indexOf(re),re.pattern().size, match.captured(i));
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用(我实际上放弃了).必须有一种更方便的方式.为了简单起见和代码可读性,我想知道采用最少行代码完成此任务的方法.
谢谢.
QString myString("School is LameCoolLame and LameRadLame");
myString.replace(QRegularExpression("Lame(.+?)Lame"),"\\1");
Run Code Online (Sandbox Code Playgroud)
上面的代码按预期工作.在您的版本中,您忘记了逃脱转义字符本身.
归档时间: |
|
查看次数: |
1045 次 |
最近记录: |