I am working on Qt using string. How can I get a string between 2 characters are '#' and ':'
my strings below:
#id:131615165
#1:aaa,s1
#23:dd
#526:FE
the result that I want to get is: "id", "1", "23", "526".
Many thanks for any help.
QRegularExpression:QRegularExpression regex("^#(.+?):");
qDebug() << regex.match("#id:131615165").captured(1);
^ matches the start of a line# matches the # character(.+?) is a capture group where:
. matches any character except line terminators+ matches one or more characters? is a "lazy" match to handle situations where multiple colons are present in the string.: matches the : characterI highly recommend an online regex-tester to start with the regex, for example: https://regex101.com
Using the following code you can capture the data from a QString:
QString input = "#id:131615165";
QRegExp test("#(.*):");
if(test.exactMatch(input))
{
qDebug() << "result:" << test.cap(1);
}
':', then get the sub-string up to that position, and skip the first character (the'#'). It's simple and easy to write and debug.