user64675
user64675

Reputation: 366

PCRE Regex - exclude some cases

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

Regex takes all mesages which looks like this:

[Help] John: ...

Problem is that chat has messages which look like this:

[      ] Tehnical information: ...
[ >Important< ] ...
[ >>>  ] ...
[ <<<  ] ...

So, I need to exclude messages, where:

Spaces and signs '>', '<' can exists in other cases and if there is other characters such numbers or something then this messages should be taken.

[General] John: Hi!
[ > BUFF < ] ...
[ >>> 45] ...

Upvotes: 0

Views: 115

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627469

I'd suggest

^(\[(?![ <>]*(?:Important[ <>]*)?])(.*)]) (.*)$

See the regex demo

The (?![ <>]*(?:Important[ <>]*)?]) negative lookahead will fail your regex match if there are just spaces inside or if there is Important word preceded or followed with any spaces, < or > in any order.

Note that the lookahead is placed right after the opening [ which reduces redundant backtracking if placed before and included [.

Pattern details:

  • ^ - start of string
  • (\[ - Start of Group 1, opening [
  • (?![ <>]*(?:Important[ <>]*)?]) - a negative lookahead that fails the match if the following is matched:
    • [ <>]* - 0+ characters: space, < or >
    • (?:Important[ <>]*)? - 1 or 0 sequences of
      • Important - literal word
      • [ <>]* - 0+ characters: space, < or >
    • ] - closing ]
  • (.*) - Group 2, matching 0+ characters other than a newline
  • ]) - End of Group 1, ] matches closing ]
  • (.*) - Group 3, matching 0+ characters other than a newline
  • $ - end of string

Upvotes: 1

ndnenkov
ndnenkov

Reputation: 36110

/^(?!\[[ <>]*(?:Important)?[ <>]*\])(\[(.*)\]) (.*)$/
  • (?!) - a negative lookahead to ensure that
  • \[\] - the thing inside the square brackets is not
  • [ <>]*(?:Important)?[ <>]* - just a variable number (*) of spaces, < and > with an optional (?) occurrence of the word Important inside.

Upvotes: 1

Related Questions