Search code examples
regex

Regex - Does not contain '<' &' >'Characters


I need a regex to match if anywhere in a sentence there is either < or >.

If either < or > are in the string then it must return true.

If both < and > are in the string in this order then it must return false.

example :- I am using '<'JAVA'>' regex.

If both > and < are in the string in this order then it must return true.

example :- I am using >JAVA< regex.

Thanks for the help.


Solution

  • Use [<>] to match either < or >. Use the negative lookahead (?!.*<.*>) to disallow both of them in this order.

    Then combine them:

    ^(?!.*<.*>).*[<>]
    

    DEMO