0

I'm trying to use Linkify but can't seem to get the Pattern.compile() part down.

Let's say i have a string like blah blah hi Hello world! can you match me?

i want to match and linkify ONLY Hello world! and nothing else. i was trying to play around with this regex: Pattern pattern = Pattern.compile("\\bHello world!\\b"); but it doesn't seem to be matching at the exclamation mark.

what's the best way to match for an EXACT sequence of characters?

David T.
  • 22,301
  • 23
  • 71
  • 123
  • 2
    Look into what `Pattern.quote()` does. – Dawood ibn Kareem Apr 03 '14 at 22:45
  • @DavidWallace you should write it as an answer so i can upvote it. i needed to have done `Pattern pattern = Pattern.compile(Pattern.quote("Hello world!"));` – David T. Apr 03 '14 at 22:48
  • I'm not convinced that's what your problem was, though. `!` isn't supposed to be special in a regular expression. Chances are you had a different bug, which you've now unknowingly removed. – Dawood ibn Kareem Apr 03 '14 at 22:56
  • @DavidWallace feel free to try out the pattern with the `!` and then without the `!`. that seemed to be the only problem for me – David T. Apr 03 '14 at 22:57
  • 1
    `\b` does not match the boundary after `!` because `!` is not part of the `\w` character class and thus doesn't form a word boundary. – CAustin Apr 03 '14 at 23:01

1 Answers1

0

Using regexs, you can use ^ at the beginning and $ at the end:

Pattern pattern = Pattern.compile("^Hello world!$");

Also this question is a repeat of this.

Loading BG
  • 131
  • 2
  • 9