Search code examples
regexsyntax-highlightingoniguruma

Match all instances of a character preceded by '/'


for example, I might have the string

/zombie nimble zombie quick
Plants vs Zombies reference

and I want to match every 'e' but only from the phrase "zombie nimble zombie quick", as it is preceded by a forward slash.

I can get the contents of the string preceded by the forward slash fairly easily with \/.*.
I can also match the first instance of 'e' in the correct string with \/.*?\Ke

But I want to match every instance of 'e' in the correct string in a way that's friendly for VSCode syntax highlighting, which afaik is the .NET flavour

-Jam


Solution

  • If you are using PCRE, you may try the following regex:

    ^(?!\/).*(*SKIP)(*F)|e
    
    • ^(?!\/).* a line doesn't start with a /
    • (*SKIP)(*F) skips the consumed text so far, which is the entire line
    • | or
    • e matches e, if the e didn't get skipped, means it's inside a line that starts with /

    See the test cases


    Edit

    Thanks for Jan's advice.

    If the / doen't always start from the beginning of a line, you may try

    ^[^\/]*(*SKIP)(*F)|e
    

    See the test cases