• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in word boundaries  RSS feed

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help in word boundaries. Please let me know why is below example not working

I have a string "abc:12345"
and my regex is \B\d\B
the output is 2

and i understand this. Now i thought of capturing the whole digits in the string, i.e the string is "abc:12345" and i wanted to capture the digits in this string i.e 12345. I tried with the below regex

\B\d*\B and this time it prints null.

Could anyone please explain how is this pattern working and why is it printing null. Also, please let me know how can i get the desired output i.e extract 12345 from abc:12345 .

Thanks in advance
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maan Suraj wrote:
Could anyone please explain how is this pattern working and why is it printing null. Also, please let me know how can i get the desired output i.e extract 12345 from abc:12345 .


Can you show us a bit of your program? The regex engine doesn't print anything, so we have no idea what you mean by "output is 2" and "prints null". So, we need to see what you are trying to print.

Henry
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output is

prints**2


Now lets change the above pat object to

Pattern pat=Pattern.compile("\\B(\\d)*\\B");
Rest all remains same

now output is

prints**null



Also please tell that which regex can i use to extract the number from the text above
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maan Suraj wrote:
output is

prints**2



Your pattern is ... a non-word boundary, followed by a digit, followed by a non-word boundary. You also have a grouping around the digit.

The first place that this occurs is at location 5. The match is the digit "2", which is surrounded by "1" and "3", both of which forms non-word boundaries.

Maan Suraj wrote:
Now lets change the above pat object to

Pattern pat=Pattern.compile("\\B(\\d)*\\B");
Rest all remains same

now output is

prints**null



Your pattern is ... a non-word boundary, followed by zero or more digits, followed by a non-word boundary. You also have a grouping around the single digit.

The first place that this occurs is at location 1. The match is a zero length string, which is surrounded by "a" and "b", both of which forms non-word boundaries. And since the match is zero length, group 1 isn't needed, which is why it is null.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maan Suraj wrote:
Also please tell that which regex can i use to extract the number from the text above


Without changing the rest of your program (using non-digit boundaries) ...




However, you actually don't even need to worry about the "boundaries" at all...



Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!