Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex Word Boundaries

Hi I have the following code which is meant to find the word "is" but not when it is within another string so the word "this" should not return a match so I use \b. But the following code does not find a match and I cant figure out why?

public static void main(String[] args) {
    String a = "This island is beautiful.";
    Pattern p = Pattern.compile("\bis\b");
    Matcher m = p.matcher(a);

    while(m.find()){

        System.out.println(a.substring(m.start(), m.end()));
    }

}
like image 870
user550 Avatar asked Jan 27 '14 19:01

user550


People also ask

How does word boundary work in regex?

Introduction to the Python regex word boundary Before the first character in the string if the first character is a word character ( \w ). Between two characters in the string if the first character is a word character ( \w ) and the other is not ( \W – inverse character set of the word character \w ).

What is \b in Java regex?

The subexpression/metacharacter “\b” matches the word boundaries when outside the brackets. Matches the backspace (0x08) when inside the brackets.

What is word boundary \B?

A word boundary \b is a test, just like ^ and $ . When the regexp engine (program module that implements searching for regexps) comes across \b , it checks that the position in the string is a word boundary.


1 Answers

Double escape it:

Pattern p = Pattern.compile("\\bis\\b");

Regex in Java requires you to doubly escape certain special regex symbols, one escaping for Java and another for underlying regex engine.

like image 131
anubhava Avatar answered Sep 30 '22 10:09

anubhava