Unsolved Substring replacement and escape syntax
-
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
http://myserver.com/share/$user/$locale
here
$user
and$locale
are substitution variables which should be replaced with corresponding values. In this case I can just use QStringreplace()
method. Another example/media/$user/docs/$$version
Here only
$user
is a substitution variable and should be replaced,$$version
is 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.
$user
,$version
,$locale
, etc.How I can implement such functionality?
-
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.
-
Hi,
To add to @koahnig, go directly with QRegularExpression, QRegExp has been deprecated with Qt 5.