I want to parse this string
[[delay-4]]Welcome! [[delay-2]]Do you have some questions for us?[[delay-1]] Please fill input field!
I need to get something like this:
[
[0] => '[[delay-4]]Welcome!',
[1] => '[[delay-2]]Do you have some questions for us?',
[2] => '[[delay-1]] Please fill input field!
];
String can also be something like this (without [[delay-4]] on beginning):
Welcome! [[delay-2]]Do you have some questions for us?[[delay-1]] Please fill input field!
Expected output should be something like this:
[
[0] => 'Welcome!',
[1] => '[[delay-2]]Do you have some questions for us?',
[2] => '[[delay-1]] Please fill input field!
];
I tried with this regex (https://regex101.com/r/Eqztl1/1/)
(?:\[\[delay-\d+]])?([\w \\,?!.@#$%^&*()|`\]~\-='\"{}]+)
But I have problem with that regex if someone writes just one [
in text, regex fails and if I include [
to match I got wrong results.
Can anyone help me with this?
In your pattern
(?:[[delay-\d+]])?([\w \,?!.@#$%^&*()|`]~-='\"{}]+)
there is no opening [
in the character class. The problem is that if you add it, you get as you say wrong results.
That is because after matching after matching delay, the character class in the next part which now contains the [
can match the rest of the characters including those of the delay part.
What you could do is to add [
and make the match non greedy in combination with a positive lookahead to assert either the next match for the delay part or the end of the string to also match the last instance.
If you are not using the capturing group and only want the result you can omit it.
(?:\[\[delay-\d+]])?[\w \\,?!.@#$%^&*()|`[\]~\-='\"{}]+?(?=\[\[delay-\d+]]|$)