• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Needed help in composing an regular expression

 
Mahendran Aiyappan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why the “*”? Why the “?” after the whitespace? Why the [] around \\s? What does [^\\d\\w] mean?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic