Home > Enterprise >  Need a very specific RegEx Filter
Need a very specific RegEx Filter

Time:01-08

Alrighty, so. I need a single regex that catches all cases of "[variable]" except for "[Swear]". I got the first part figured out,^\[[a-zA-Z] \] works well enough for me. I also somewhat got the second part - ^((?!\[Swear\]).)*$gets everything except for the specific phrase "[Swear]"

Now, I sadly dont know how to combine the two - I want it to catch anything that has square brackets around it, except for when the content of the brackets is "Swear"

If you need more details I can try to be more specific

CodePudding user response:

Use

\[(?!Swear\])[a-zA-Z] \]

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \[                       '['
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    Swear                    'Swear'
--------------------------------------------------------------------------------
    \]                       ']'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-zA-Z]                 any character of: 'a' to 'z', 'A' to 'Z'
                           (1 or more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \]                       ']'
  •  Tags:  
  • Related