如何在拆分QString时转义分隔符
How to escape the delimiter when splitting a QString?
如何用字符(例如:'+'
)拆分QString
,并且在转义该字符时不拆分:'+'
?
谢谢!
根据要求,提供更多细节:
要拆分的字符串:"a++"
分隔符:'+'
所需输出:"a"
、"+"
您需要将globalMatch
与一个正则表达式一起使用以进行拆分,该表达式选择除非转义'+'
:之外的所有内容
(?:[^\+]|\.)*
实时示例
因此,给定QString foo
,您可以使用QRegularExpressionMatchIterator
:迭代列表
QRegularExpression bar("((?:[^\\\+]|\\.)*)");
auto it = bar.globalMatch(foo);
while(it.hasNext()){
cout << it.next().captured(1).toStdString() << endl;
}
在C++11中,您还可以使用cregex_token_iterator
:
regex bar("((?:[^\\\+]|\\.)+)");
copy(cregex_token_iterator(foo.cbegin(), foo.cend(), bar, 1), cregex_token_iterator(), ostream_iterator<string>(cout, "n"));
实时检查
不幸的是,您既没有Qt5,也没有C++11,也没有Boost,您可以使用QRegExp
:
QRegExp bar("((?:[^\\\+]|\\.)*)");
for(int it = bar.indexIn(foo, 0); it >= 0; it = bar.indexIn(foo, it)) {
cout << bar.cap(1).toStdString() << endl;
}
如果不使用"+"作为分隔符,而是使用空格作为分隔符。。。splitArgs
为您工作:
https://stackoverflow.com/a/48977326/2660408
相关文章:
- 如何用转义符替换字符串中的所有特殊字符
- 当用户键入分隔符时,停止getline()输入
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 如何在 c++ 中使用 ',' 作为 getline 分隔符
- 带有多个字符分隔符的正则表达式
- 使用 char 分隔符解析C++中的字符串,但将可重复的字符保留为每个解析的子字符串 (C++ STL) 中的分隔符
- 字符串开头的分隔符
- 如何在C++中将 Python 字符串转换为其转义版本?
- 如何在C++中用'\'替换''来处理转义序列?
- 尝试将 c 字符串数组与分隔符连接起来
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 根据新的行分隔符从字符串中删除子字符串
- 如何解释C++字符串中的 \u 转义码?
- 同一分隔符之间的多个子字符串
- 通过分隔符分隔包含 UTF-16 BE 文本的uint8_t数组
- PostgresSQL - SQL Ready 语句与字符串转义,防止 SQL 注入攻击
- 字符串流分隔符
- 有没有办法获取用户操作系统的ANSI转义码?
- 如何在 std::getline 中自定义分隔符
- 如何在拆分QString时转义分隔符