Search code examples
regexmobaxterm

How to exclude a word using Regex for MobaXterm Syntax Highlighting?


I created a regex expression to match any string surrounded by quotation marks in a log such as "example" but exclude the word heartbeat if found.

[^0-9A-Za-z_&-]("(?!heartbeat)[A-Za-z0-9_.&?=%~#{}()@+-:]*")[^A-Za-z0-9_-]

I verified the expression in regex101 but once in MobaXterm it does not work. My assumption is MobaXterm does not handle Negative Lookaheads.

Keep in mind the following does work:

[^0-9A-Za-z_&-]("[A-Za-z0-9_.&?=%~#{}()@+-:]*")[^A-Za-z0-9_-]

Is there an alternative to what I am trying to achieve?


Solution

  • You can make some hard to read regexes such as:

    "(?:[^h].*|h[^e].*|he[^a].*|hea[^r].*|etc...)"

    (replace .* with the second character class) but another option would be to write "heartbeat"|([A-Za-z0-9_.&?=%~#{}()@+-:]*). When the string is "heartbeat" this will skip the capture group but that only works if your program is specifically looking for the capture group.

    You can also place \w and \d in your character classes to make them simpler: [^\d\w&-](?:"heartbeat"|("[\w\d.&?=%~#{}()@+-:]*"))[^\d\w-]