Question on using QRegularExpression
-
Hi!
I read a bunch of posts and read the docs but couldn't seem to find an answer to the issue I have.
In Qt 5 we were able to use QRegExp with a code fragment shown below
QRegularExpression digits("\d+"); // '\d+' (one or more digits)
QStringList attributeValues;for(int i = 0; i < attributeValues.size(); i++)
{
if(digits.exactMatch(attributeValues[i]) && !attributeValues[i].contains(".")) //make sure floating point numbers are not included
{
intAttributeValues.append(attributeValues[i].toInt());
}
}I'd like to implement the same logic using QRegularExpression
I know I can do something like this:
QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));But how do implement an exact match as above with the if statement?
Any help would be appreciated!
-
@leinad said in Question on using QRegularExpression:
but how do I check if my variable has attributeValues[i] has digits?
Sorry, I wrote the idea earlier but not the exact correct thing! Should be:
QRegularExpression digits(QRegularExpression::anchoredPattern("\\d+")); for(int i = 0; i < attributeValues.size(); i++) if (digits.match(attributeValues[i]).hasMatch())
Is that what you mean?
-
Hi!
I read a bunch of posts and read the docs but couldn't seem to find an answer to the issue I have.
In Qt 5 we were able to use QRegExp with a code fragment shown below
QRegularExpression digits("\d+"); // '\d+' (one or more digits)
QStringList attributeValues;for(int i = 0; i < attributeValues.size(); i++)
{
if(digits.exactMatch(attributeValues[i]) && !attributeValues[i].contains(".")) //make sure floating point numbers are not included
{
intAttributeValues.append(attributeValues[i].toInt());
}
}I'd like to implement the same logic using QRegularExpression
I know I can do something like this:
QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));But how do implement an exact match as above with the if statement?
Any help would be appreciated!
@leinad
I think I figure it out. Can someone please confirm if it makes sense?
QStringList attributeValues;
QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));for(int i = 0; i < attributeValues.size(); i++)
{
QRegularExpressionMatch match = digits.match(attributeValues[i]);
bool intMatch = match.hasMatch();
if(intMatch && !attributeValues[i].contains("."))
{
intAttributeValues.append(attributeValues[i].toInt());
}
} -
@leinad
I think I figure it out. Can someone please confirm if it makes sense?
QStringList attributeValues;
QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));for(int i = 0; i < attributeValues.size(); i++)
{
QRegularExpressionMatch match = digits.match(attributeValues[i]);
bool intMatch = match.hasMatch();
if(intMatch && !attributeValues[i].contains("."))
{
intAttributeValues.append(attributeValues[i].toInt());
}
} -
I tried to figure out how to do that but couldn't find a way. Is this better?
I assume what I did makes sense?
Thanks!QStringList attributeValues; QRegularExpression digits(QRegularExpression::anchoredPattern("\d+")); for(int i = 0; i < attributeValues.size(); i++) { QRegularExpressionMatch match = digits.match(attributeValues[i]); bool intMatch = match.hasMatch(); if(intMatch && !attributeValues[i].contains(".")) { intAttributeValues.append(attributeValues[i].toInt()); } }
-
I tried to figure out how to do that but couldn't find a way. Is this better?
I assume what I did makes sense?
Thanks!QStringList attributeValues; QRegularExpression digits(QRegularExpression::anchoredPattern("\d+")); for(int i = 0; i < attributeValues.size(); i++) { QRegularExpressionMatch match = digits.match(attributeValues[i]); bool intMatch = match.hasMatch(); if(intMatch && !attributeValues[i].contains(".")) { intAttributeValues.append(attributeValues[i].toInt()); } }
@leinad
Firstly, you know that your"\d+"
is wrong in both cases (you may be getting away with it), it should always be"\\d+"
.As you say:
if (regExp.exactMatch())
needs to be translated toif (regularExpression.match(QRegularExpression::anchoredPattern("\\d+")).hasMatch())
. -
@leinad
Firstly, you know that your"\d+"
is wrong in both cases (you may be getting away with it), it should always be"\\d+"
.As you say:
if (regExp.exactMatch())
needs to be translated toif (regularExpression.match(QRegularExpression::anchoredPattern("\\d+")).hasMatch())
.@JonB Thanks, I had a typo when I typed the code into this page. I do indeed have
"\\d+"
In your logic you have
if (regularExpression.match(QRegularExpression::anchoredPattern("\\d+")).hasMatch())
but how do I check if my variable has attributeValues[i] has digits? It looks like my logic is correct I just broke it down into smaller code segments. -
@JonB Thanks, I had a typo when I typed the code into this page. I do indeed have
"\\d+"
In your logic you have
if (regularExpression.match(QRegularExpression::anchoredPattern("\\d+")).hasMatch())
but how do I check if my variable has attributeValues[i] has digits? It looks like my logic is correct I just broke it down into smaller code segments.@leinad said in Question on using QRegularExpression:
but how do I check if my variable has attributeValues[i] has digits?
Sorry, I wrote the idea earlier but not the exact correct thing! Should be:
QRegularExpression digits(QRegularExpression::anchoredPattern("\\d+")); for(int i = 0; i < attributeValues.size(); i++) if (digits.match(attributeValues[i]).hasMatch())
Is that what you mean?
-
@leinad said in Question on using QRegularExpression:
but how do I check if my variable has attributeValues[i] has digits?
Sorry, I wrote the idea earlier but not the exact correct thing! Should be:
QRegularExpression digits(QRegularExpression::anchoredPattern("\\d+")); for(int i = 0; i < attributeValues.size(); i++) if (digits.match(attributeValues[i]).hasMatch())
Is that what you mean?
-