Solved QRegularExpressionMatch
-
is it possible to pass in a variable as part of the regex matching line?
can you explain better? maybe with an example?
Also I don't seem to have the QRegularExpression Tool example (mentioned above).
I use https://regex101.com/ same principle, better colours and allows you to unit-test too
-
Sorry, yes - I mean in the regular expression line:
instrumentRegExp(R"(,?\sInstrument(\d+)(?:Status)?\s(?:=\s*([-+]?\d*.?\d+(?:[eE][-+]?\d+)?))?)"
where its looking for an Instrument, is there a way to do something like this:
instrumentRegExp(R"(,?\sMyStringVariableHere(\d+)(?:Status)?\s(?:=\s*([-+]?\d*.?\d+(?:[eE][-+]?\d+)?))?)"
Regarding regular expression testing, I have realized that my text editor supports regex searching. I think it would be just a few tweaks to move them from there to QT.
Thanks again!
-
@MScottM said in QRegularExpressionMatch:
where its looking for an Instrument, is there a way to do something like this:
instrumentRegExp(R"(,?\sMyStringVariableHere(\d+)(?:Status)?\s(?:=\s*([-+]?\d*.?\d+(?:[eE][-+]?\d+)?))?)**"Well, just insert them as a variable to a
QString
before you pass the QString toQRegularExpression()
.QString myString("Static text with %1 some %2 variables").arg("foo").arg("bar");
-
Dang...geniuses...all of you.
Thanks! Will be testing tonight.
-
Side note:
@Joel-Bodenmann Let's stare into the abyss of
arg().arg()
: http://doc.qt.io/qt-5/qstring.html#arg-12 -
@VRonin That was interesting - didn't know about that one :p
-
You're a overplaying it a bit, aren't you? :)
I mean I knew of that pitfall, but there's nothing wrong in usingarg().arg()
as long as you're aware of this peculiarity, right? -
Warning: philosophical talk below.
I'm actually surprised to hear this from you, @kshegunov. I used
arg
above so I'm not 100% dogmatic on it but lets take a good look at it:arg
is a search and replace so it's slower thanQString::operator+
(if you concatenate a lot you can useoperator%(const QString&,const QString&)
inQStringBuilder
). This should be reason enough not to use it. If you add thearg().arg()
pitfall, that applies to any string you don't have complete control over all the aspect of input, it makes it almost evil.The only reason to use
arg()
I can see is internationalisation, but, even then, you have to take care of just usingarg(const QString&)
as numbers shown to the user should be processed byQLocale
and be mindful that if you callarg
more than once on the same string you are opening yourself to a translator possibly introducing what looks like a logic bug in your code -
Success!!
This piece of code is working now! Nearly exactly as VRonin posted...Plus I learned something from the discussion above: I have a lot to learn :)
I wanted to ask about the capitol R that starts off the RegEx string - I figured out that it lets you use the single escape characters, but I couldn't find it documented anywhere...?
Thanks to all and best regards!
Scott
-
@MScottM said in QRegularExpressionMatch:
I wanted to ask about the capitol R that starts off the RegEx string - I figured out that it lets you use the single escape characters, but I couldn't find it documented anywhere...?