This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java Regexp matching - not working in loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Regexp matching - not working in loop" Watch "Java Regexp matching - not working in loop" New topic
Author

Java Regexp matching - not working in loop

cyber aishu
Greenhorn

Joined: Mar 06, 2011
Posts: 2
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

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

Joined: Mar 06, 2011
Posts: 2
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 !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Regexp matching - not working in loop
 
Similar Threads
Need help regarding with "\n"
how to use grep in regular expression java
Regex: find() method
regexp problem - smallest possible match on string
Regex Doubt