Search code examples
javaregexequalscontain

Java String contains a special Char but not even one more Char


I am looking for every single URL, which is linked as "eye" in a html Document. I am using a regex pattern, because a simple contains is no solution at this point. So I got a pattern like this

Pattern:: href=\"(https?://)?[a-zA-z0-9?/&=\"+-_\\.# ]*>[Ee]ye

It works... fine... more or less... Because I get more than any URL linked as "Eye" or "eye". I'll get URLs which are linked as "eyebrights" or "eyewears", too, but that's not what I want.

Is there any way to say "get me this and ignore it, when there is more than I want"?


Solution

  • Add \b after eye:

    href=\"(https?://)?[a-zA-z0-9?/&=\"+-_\\.# ]*>[Ee]ye\\b
    

    \b: assert position at a word boundary.