This week's book giveaway is in the JavaScript forum.
We're giving away four copies of JavaScript Promises Essentials and have Rami Sarieddine on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Needed help in composing an regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Needed help in composing an regular expression" Watch "Needed help in composing an regular expression" New topic
Author

Needed help in composing an regular expression

Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Hi Guys,

This is regarding composing an regular expression to satisfy the given conditions.

The conditions are:
1. I wanted to return true/false if a particular word is present in the paragraph.
2. The word can be anywhere (in the beginning, middle, or end)
3. It should return only for whole words with an exception. The word can precede (or) follow by only one special character such as ,.;()[]{} etc
4. Also it is case insensitive search.

In the below code I am searching for a word Positive. I have hardcoded the string in the regex. Ideally in this case the output should be false, but it is returning true.
So I am not sure how to do this.



Thanks in advance.
Mahendran

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

I haven't checked the whole expression, but there's a couple of things that stand out:

1. You've converted the input to upper case, but you've got references to lower-case letters in your regular expression

2. [POSITIVE\b]+ doesn't look for the word "POSITIVE". It looks for any letters in the word "POSITIVE". I think you're confusing square and regular brackets. And with [\\s]? - why are the brackets there?
Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Matthew Brown wrote:
1. You've converted the input to upper case, but you've got references to lower-case letters in your regular expression

I am not sure I understand it correctly. Please correct me if I am wrong. My regular expression is [^a-z]*[\\s]?[^\\d\\w]?[POSITIVE\b]+[^a-z]* which has POSITIVE in caps.

Matthew Brown wrote:
2. [POSITIVE\b]+ doesn't look for the word "POSITIVE". It looks for any letters in the word "POSITIVE". I think you're confusing square and regular brackets. And with [\\s]? - why are the brackets there?

I am new to regular expression. What is regular brackets mean? is it ( ) ?

Thanks,
Mahendran
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
I would suggest the Java Tutorials section for starting regular expressions. Regular expressions can be complicated enough to be a whole topic by themselves.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8176
    
  23

Mahendran Aiyappan wrote:I am new to regular expression. What is regular brackets mean? is it ( ) ?

Yes, but in your case, if you're looking for a specific word, you may not even need those. Also, if you're using Pattern, you really only have to worry about the string you're trying to match, not all the stuff around it (unless you also want to validate the entire string).

I'll give you this for free: "(i?)" can be used to do a case-insensitive search.

I suggest you have a really good look through the docs for java.util.regex.Pattern or, as Campbell suggests, the tutorials, and come back if you have any more questions.

Winston
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

Mahendran Aiyappan wrote:
Matthew Brown wrote:
1. You've converted the input to upper case, but you've got references to lower-case letters in your regular expression

I am not sure I understand it correctly. Please correct me if I am wrong. My regular expression is [^a-z]*[\\s]?[^\\d\\w]?[POSITIVE\b]+[^a-z]* which has POSITIVE in caps.

Yes. But what about [^a-z]?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
And why the “*”? Why the “?” after the whitespace? Why the [] around \\s? What does [^\\d\\w] mean?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Needed help in composing an regular expression