user5551022 user5551022 - 1 year ago 73
PHP Question

Regex any characters except one specific, with break line PHP

I would like to build a regex that grep all characters between [[xxx]], with new line.



But this regex grep :


I would like :

"dgghr" and "dfgdfgfd

I tried to exclude "]" character, without success :(
Thank you for all.


I've just finded! Here the solution :
String :


Regex :


Output :

array('dgghr', 'dfgdfgfd\ndgfsdgdg')

Thank you MariaDeleva and splash58

Answer Source

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 :


You can try it on regex101.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download