Hello,
In Java I use this regular expression \b\w+\b as a pattern in order to find words but the boundary does not react to newlines. For example, in the following text:
hello and bye
and
blah
it finds the words below:
hello
byeandblah
and
Could someone tell how to fix that?
p.s. Just in case it's a clue, the example text does not have empty lines when I view it in gedit, they only appear over here, after I paste the text and submit the post.
Works fine for me:
$
$
$ # show the Java program
$ cat -n RegexTest.java
1 import java.util.regex.Matcher;
2 import java.util.regex.Pattern;
3 public class RegexTest {
4 public static void main(String[] args) {
5 String str = "hello and bye\nand\nblah";
6 String ptn = "\\b\\w+\\b";
7 int num = 0;
8 Pattern pattern = Pattern.compile(ptn);
9 Matcher matcher = pattern.matcher(str);
10 System.out.println("======= String for inspection =======");
11 System.out.println(str);
12 System.out.println("========================================");
13 System.out.println("======= Pattern to be searched =======");
14 System.out.println(ptn);
15 System.out.println("========================================");
16 boolean found = false;
17 while (matcher.find()) {
18 num++;
19 System.out.println("Match # "+num+"\t:\t"+matcher.group()+"\t=>\tstarts at "
20 + "index "+matcher.start()+" and ends at index "+matcher.end());
21 found = true;
22 }
23 if (!found) {
24 System.out.println("No match found");
25 }
26 }
27 }
$
$ # compile
$ javac RegexTest.java
$
$ # run
$ java RegexTest
======= String for inspection =======
hello and bye
and
blah
========================================
======= Pattern to be searched =======
\b\w+\b
========================================
Match # 1 : hello => starts at index 0 and ends at index 5
Match # 2 : and => starts at index 6 and ends at index 9
Match # 3 : bye => starts at index 10 and ends at index 13
Match # 4 : and => starts at index 14 and ends at index 17
Match # 5 : blah => starts at index 18 and ends at index 22
$
$
tyler_durden