This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android 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: 4343
    
    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: 38033
    
  22
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: 7549
    
  18

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: 4343
    
    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: 38033
    
  22
And why the “*”? Why the “?” after the whitespace? Why the [] around \\s? What does [^\\d\\w] mean?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Needed help in composing an regular expression
 
Similar Threads
Need to tokenize a String , but i need to keep what comes between "and"
hash map error
textarea tag - Entering string problem
Hot to deal with OutOfMemoryError
find replace in file with regular expression