2

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

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
Jam
  • 476
  • 3
  • 9

2 Answers2

2

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

Hao Wu
  • 17,573
  • 6
  • 28
  • 60
  • 1
    Maybe the `/` does not start right at the beginning (use a neg. lookahead then) but +1 for `(*SKIP)(*FAIL)`. See [**this demo**](https://regex101.com/r/6OzxcS/1/) for a slash not starting right at the beginning of the line. Using other delimiters (e.g. `~`) you could as well use `^(?!/)...`, see https://regex101.com/r/gseRUF/1 – Jan May 24 '21 at 06:35
  • i want to accept both answers because they're both good but this one can also be easily used for finding specific words after the fwd slash – Jam May 24 '21 at 23:37
2

Using PCRE you could go for

(?:\G(?!\A)|/)[^e\n]*\Ke

See a demo on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • 1
    ++ For use of `\G` and keeping OP's original approach – Hao Wu May 24 '21 at 07:01
  • i ended up using this in a different expression to match method parameters <3 this is it if you're curious `(?:\G|method\(|function\().*?\K(?:(["'])(?:(?<!\\)\\(?:\\\\)*\1|(?!\1).)*?\1|[^,\s=("']+)\s*(?=[,\)])` – Jam May 25 '21 at 03:10