QString: replace first and second appear string
-
wrote on 24 Apr 2019, 10:08 last edited by
Replace first "~~" to "<s>", second "~~" to "</s>". Example: ~~Delete~~ To <s>Delete</s>
-
Is there a question somewhere in here?
There is no magic API that will do it for you, you need to come up with an algorithm to replace such strings. An example (probably slow):
const QLatin1String delimiter("~~"); QString string("~~Delete~~"); bool odd = false; while (string.contains(delimiter)) { if (odd) { string.replace(delimiter, "</s>"); } else { string.replace(delimiter, "<s>"); } odd != odd; }
-
wrote on 24 Apr 2019, 11:29 last edited by JonB
@sonichy , @sierdzio
I'm going to throw my hat in, and ask a question.Although the OP may not want to use regular expressions to do this, and @sierdzio's solution is doubtless perfectly acceptable, I would. It intrigued me, so I had a go (e.g. use https://regex101.com/, highly recommended). In preparation for the substitution on
~~Delete~~
I got as far as:(~~)((?!~~).*)(~~)
I'm assuming I need the negative lookahead construct
(?!~~)
to prevent gobbling any middling~~
s before the next one. My expression is not right though.It's very difficult to Google for, because it's so hard to describe. Would an re expert care to show what the correct re for the match/substitution would be here, please?
EDIT: Just thought it through again. Is this to be solved simply via the non-greedy construct
.*?
:~~(.*?)~~
and then we can just replace globally with
<s>\1</s>
, doing the lot in one go?So I think (untested):
QString s = "000~~abc~~~~Delete~~def~~ghi~~999"; s.replace(QRegularExpression("~~(.*?)~~"), "<s>\\1</s>");
-
Yes, regular expressions are a good way to solve this as well, and your example seems good (I have not tested it, though).
One thing I dislike about regexp is that it is "write-only" code - very hard to understand and maintain later. It is a valid solution, and will probably work faster than my attempt.
-
Yes, regular expressions are a good way to solve this as well, and your example seems good (I have not tested it, though).
One thing I dislike about regexp is that it is "write-only" code - very hard to understand and maintain later. It is a valid solution, and will probably work faster than my attempt.
-
@sonichy , @sierdzio
I'm going to throw my hat in, and ask a question.Although the OP may not want to use regular expressions to do this, and @sierdzio's solution is doubtless perfectly acceptable, I would. It intrigued me, so I had a go (e.g. use https://regex101.com/, highly recommended). In preparation for the substitution on
~~Delete~~
I got as far as:(~~)((?!~~).*)(~~)
I'm assuming I need the negative lookahead construct
(?!~~)
to prevent gobbling any middling~~
s before the next one. My expression is not right though.It's very difficult to Google for, because it's so hard to describe. Would an re expert care to show what the correct re for the match/substitution would be here, please?
EDIT: Just thought it through again. Is this to be solved simply via the non-greedy construct
.*?
:~~(.*?)~~
and then we can just replace globally with
<s>\1</s>
, doing the lot in one go?So I think (untested):
QString s = "000~~abc~~~~Delete~~def~~ghi~~999"; s.replace(QRegularExpression("~~(.*?)~~"), "<s>\\1</s>");
-
@sonichy you will need to learn som regex basics.
\1
is the string captured between(
and)
, and in C++ you have to escape the backslash, hence\\1
. -
wrote on 28 Apr 2019, 05:06 last edited by Kent-Dorfman
This is exactly what regular expressions are for...Qt may be overkill if OP is just processing a markup text document.
simplest perl example, not meant to be a comprehensive solution:
echo "something ~~token~~ more" | perl -ne "s/(~~)(.*)(~~)/<s>\$2<\/s>/; print;"output:
something <s>token</s> moreSorry if mentioning a different tool is blasphemy. LOL
-
@sonichy you will need to learn som regex basics.
\1
is the string captured between(
and)
, and in C++ you have to escape the backslash, hence\\1
. -
wrote on 30 Apr 2019, 11:50 last edited by
@sonichy
As you please. With due respect to @Kent-Dorfman's suggestion, apart from the fact that it's written in Perl it will not correctly deal with multiple required substitutions on any given line. If you wish to try a regular expression for your situation I humbly suggest you try the correct Qt one I gave earlier, with its multi-substitution example I provided.
1/10