pelican_george
pelican_george

Reputation: 971

How to capture all regex occurrences (plus negation)

I'm just trying a few examples and I'm kind of stuck with this one:

(&[^#|amp|quot|apos])

It's capturing an extra character after '&'.

a &a text with & an &f and &#

Captures &a and &f.

How to capture & only?

Upvotes: 0

Views: 53

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

What you are looking for is negative look ahead. You can write it as

&(?!#|amp|quot|apos)

Regex Demo

Regex Explanation

  • & Matches a single &

  • (?!#|amp|quot|apos) Negative look ahead. Check if the & is not followed by # or amp or quot or apos.


What is wrong with (&[^#|amp|quot|apos])

  • [^#|amp|quot|apos] This doesn't translate to amp or quot or apos. This is a character class which translate to # or | or a or m or p etc.

Upvotes: 2

Related Questions