I have this string
blabla [IC=\"test\"]Hello, [IC=\"testing\"] this is a nested tag [EC], cya.[EC] blabla
I\'m trying to match the outer [
You could use a recursive pattern (supported in PCRE
):
\[IC[^][]*\]
(?:(?:[^][]*|(?R))*)
\[EC\]
See a demo on regex101.com.
The regular expression below should do the trick:
[^\[]*(\[.*\])[^\]]*
You can try a working demo visiting this link.
Alternatively, if you don't want to rely on capturing groups, this should be enough:
(\[.*\])
A working demo of the regular expression above can be tested here.
I dont get your decision to use the \s\S
and it just seems more confusing than it really should be.
However to fix the issue you are having simply remove the ?
...([\s\S]*?)\[EC]...
to
...([\s\S]*)\[EC]...
resulting into
\[IC=\"([\s\S]*?)\"]([\s\S]*)\[EC]\n{0,1}
however i would simply use \w
and .*
because they are easier
\[IC="(\w*)"\](.*)\[EC\]
EDIT:
Working off of the assumption that you actually need to get the string value from [IC=".."] and string inbetween. otherwise if you just need whole data then no need for the groupings
\[IC="\w*"\].*\[EC\]
Try this:
(\[IC="\w*"\].*\[EC\])
https://regex101.com/r/MVU9ni/1
Matches 0 or more characters that are located in between [IC="any_word_character_here"]
and [EC]
.