Regex match <head ...> not <header>

Viewed 113

I need a regex that can match either <head ...> or <head> but not <header>.

I currently have (<head(?!:er)*>

I succeeded in part, removing er with (?!:er) but <head ...> is still invalid.

Any regex expert could help? Thanks!

1 Answers

Your current <head(?!:er)*> pattern matches <head, then it optionally requires (due to * at the end of the lookahead construct) if the text immediately to the right is not :er (here, : is part of the lookahead pattern, not part of a lookahead construct) and then requires a > char. Since > is not :, this lookahead always returns true.

You need something like

<head(?!er>)[^<>]*>

where

  • <head - matches <head
  • (?!er>) - makes sure the text immediately on the right is not er>
  • [^<>]*> - matches any zero or more chars other than < and > and then a >.

See the regex demo.

Related