Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Regexp matching - not working in loop

 
cyber aishu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to perform a java regexp matching as follows:
I have an xml file which I parse to instantiate a rule object. The xml file also contains a regular expression, which is a field in the Rule object. Rule object is as follows :


Sample input value for str in one iteration is 'This is our house and we have a dog'
Sample regExp is '\\bour\\b'
Issue: m.find() - never returns true. I have been breaking my head over this for quite sometime now and not been able to find a solution. Please help.

Thanks in advance !!


[HENRY: Added Code Tags]
 
Henry Wong
author
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cyber aishu wrote:
Sample input value for str in one iteration is 'This is our house and we have a dog'
Sample regExp is '\\bour\\b'


Wrote a quick sample app using your test data and your code (with some syntax errors fixed), and it works.... works for me.

BTW, here's the code:


Henry
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the regular expression is being read from an XML file, there's no need to escape the backslash. That's only needed for Java string literals. So just try \bour\b.
 
cyber aishu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Matthew and Henry !!

@ Mathew: You are a life saviour !! That was the issue. I corrected my regexp to \bour\b and it worked fine !!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic