Search code examples
javaregex

Using Java regex want to match only & but not & < etc


Using Java regex pattern I want to match the & symbol. But it should not match &, < etc... (HTML encoding).


Solution

  • Try this:

    &(?![A-Za-z]+;|#[0-9]+;)
    

    This matches an & that's not followed by one or more ASCII letters and a ; or a hash symbol, followed by one or more ASCII digits and a ";".