我有字符串 str
。我想得到两个字符串('+' 和 '-'):
QString str = "+asdf+zxcv-tyupo+qwerty-yyuu oo+llad dd ff";
// I need this two strings:
// 1. For '+': asdf,zxcv,qwerty,llad dd ff
// 2. For '-': tyupo,yyuu oo
QRegExp rx("[\\+\\-](\\w+)");
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
qDebug() << rx.cap(0);
pos += rx.matchedLength();
}
我需要的输出:
"+asdf"
"+zxcv"
"-tyupo"
"+qwerty"
"-yyuu oo"
"+llad dd ff"
我得到的输出:
"+asdf"
"+zxcv"
"-tyupo"
"+qwerty"
"-yyuu"
"+llad"
如果我用 .*
替换 \\w
输出是:
"+asdf+zxcv-tyupo+qwerty-yyuu oo+llad dd ff"
最佳答案
您可以使用以下正则表达式:
[+-]([^-+]+)
参见 regex demo
正则表达式分解:
[+-]
-+
或-
([^-+]+)
- 匹配除-
和+
之外的 1 个或多个符号的捕获组。
关于c++ - Qt C++ QRegExp 解析字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33630556/