regular-expressions – Excluding Certain Cases from Matching Result

Question:

$regex = /(^)(\[(.*)\]) (.*)$/;

Regex takes all chat messages of the form:

[Помощь] Джон: ...

The problem is that the chat also contains lines like:

[      ] Служебная информация: ...
[ >Важно< ] ...
[ >>>  ] ...
[ <<<  ] ...

That is, in fact, I need to exclude lines where at the beginning between [ and ] :

  • There are only spaces and / or signs > and <
  • There is only an inscription важно , with spaces and signs < , > or without

In this case, the space, the signs > and < can be found in other cases, but if there are also other characters between [ and ] , then such a string must be taken

[Общий] Джон: Привет
[ > СТАТУС < ] ...
[ >>> 45] ...

Answer:

Something like:

$regex = /^\[(?![\s<>]*\])(.*?)\] (.*)$/;

https://regex101.com/r/cA4vH0/1

Scroll to Top