I would like to build a regex that grep all characters between [[xxx]], with new line.
[[dgghr]]
[[dfgdfgfd
dgfsdgdg]]
/\[\[([\s\S]+)\]\]/s
But this regex grep :
"df]]
[[df
d
gfdg"
I would like :
"dgghr" and "dfgdfgfd
dgfsdgdg"
I tried to exclude "]" character, without success :( Thank you for all. G.
I've just finded! Here the solution : String :
[[dgghr]]
[[dfgdfgfd
dgfsdgdg]]
Regex :
/\[\[([\s\S][^\]]+)\]\]/s
Output :
array('dgghr', 'dfgdfgfd
dgfsdgdg')
Thank you MariaDeleva and splash58 G.
To exclude a character, use a negated class : [^x]
matches any character but x
. Since ]
has a special meaning in character classes, you have to escape it : [^\]]
Now simply excluding ]
could give you undesirable results if the text bewteen double brackets can contain brackets : \[\[[^\]]*\]\]
wouldn't match [[this]test]]
.
I believe the only problem with your regex is that it is greedy : it will match as much as it can, rather than stopping to the first match, so the [\S\s]
class will match closing brackets as long as there are others that can be matched by \]\]
later.
So what I propose is to make your [\S\s]+
part lazy/non-greedy, as in the following :
/\[\[([\s\S]+?)\]\]/s
You can try it on regex101.