如何使用QT RegExp从字符串中提取子字符串列表

How to extract a list of substring from a string using QT RegExp

本文关键字:字符串 提取 列表 何使用 QT RegExp      更新时间:2023-10-16

如何使用QT RegExp从字符串中提取子字符串列表,例如,如果我有这个输入字符串"qjkfsjkdfn 54df#Sub1#sdkf ++sdf #Sub2#q qfsdf445#Sub3#sdf"我想使用"(#.+#)" RegExp获得一个包含"Sub1""Sub2""Sub3"的列表。

您可以使用以下代码:

QRegExp rx("#([^#]+)#"); // create the regular expression
string text = "qjkfsjkdfn 54df#Sub1#sdkf ++sdf #Sub2#q qfsdf445#Sub3#sdf";
int pos = 0;
while ( (pos = rx.search(text, pos)) != -1 ) // while there is a matching substring
{
     cout << rx.cap(1); // output the text captured in group 1
}