A.Thabet
A.Thabet

Reputation: 113

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

How to extract a list of substrings from a string using QT RegExp for example, if i have this input string "qjkfsjkdfn 54df#Sub1#sdkf ++sdf #Sub2#q qfsdf445#Sub3#sdf" i want to get a list that contains "Sub1", "Sub2" and "Sub3" using "(#.+#)" RegExp.

Upvotes: 0

Views: 2730

Answers (1)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

You can use the following code:

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
}

Upvotes: 3

Related Questions