Substring replacement and escape syntax
voltron last edited by
I need to replace some substring in the input string and in the same time support some sort of escape syntax. Here is an example of the input string
$localeare substitution variables which should be replaced with corresponding values. In this case I can just use QString
replace()method. Another example
$useris a substitution variable and should be replaced,
$$versionis escaped and should not be replaced.
Number of the substitution variables in the input string is unknown, I only have list of possible variables, e.g.
How I can implement such functionality?
koahnig last edited by
The replace with regular expressions are good candidates for your purpose. Certainly it is also a complex way to do the job. The $ is a special character in QRegExp requiring escaping as well.
Another way to do it step by step searching for $ in the strings.
In case you like to go for the regular expressions and you have no experience there, I recommend to start with the QRegExp example. This is ideal to syntax check your expressions.
The newer style would QRegularExpression with this example. Probably it does the same job as QRegExp.
To add to @koahnig, go directly with QRegularExpression, QRegExp has been deprecated with Qt 5.