This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Needed help in composing an regular expression  RSS feed

 
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

 
Bartender
Posts: 4568
9
  • 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
 
Marshal
Posts: 59717
187
  • 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.
 
Bartender
Posts: 10575
66
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: 4568
9
  • 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
Marshal
Posts: 59717
187
  • 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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!