• Post Reply Bookmark Topic Watch Topic
  • New Topic

lexical search using Pattern and Matcher class  RSS feed

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

Need some help with the following Query. I want to find out how I can implement
the following by using Pattern / Matcher classes.

I have a query that returns the following set of strings,

aa
abc
def
ghi
glk
gmonalaks
golskalskdkdkd
lkaldkdldldkdld
mladlad
n33ieler

What I would like do is to find out any string that starts with g and Lexical occurs after ghi. So this will return
ghi / glk / gmonalaks / golskalskdkdkd

How can I accomplish the above, by using the following two classes.

Pattern datePattern = Pattern.compile();
Matcher dateMatcher = datePattern.matcher();

Thanks a bunch...
_Shoe Maker..
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "Lexical occurs after ghi"?
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Lexigraphically means that when I am searching for strings starting with g it will match every thing that starts with "g", but when I say Lexigraphically after ghi means that I am interested in results that are lexically greater than "ghi" some thing like ghi / glk / gmonalaks / golskalskdkdkd

 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Rodeo wrote:

Lexigraphically means that when I am searching for strings starting with g it will match every thing that starts with "g", but when I say Lexigraphically after ghi means that I am interested in results that are lexically greater than "ghi" some thing like ghi / glk / gmonalaks / golskalskdkdkd



Here's how:



But looking at the variable names in your original post, I think you have over simplified your question. It looks like you're trying to numerically compare strings that represent dates using regex. This is not what regex is meant for, and it would be easier - and less error prone - if you use "normal" programming logic.

HTH.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Piet. Since you have a set of strings that you need to iterate through anyway, it would be easier to use a combination of String.startsWith and String.compareTo.

You could also subclass one of the collection classes with a method that returns all elements starting with a particular prefix.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I agree with Piet. Since you have a set of strings that you need to iterate through anyway, it would be easier to use a combination of String.startsWith and String.compareTo.

You could also subclass one of the collection classes with a method that returns all elements starting with a particular prefix.


... and if they're dates, parse them to java.util.Date objects (using the SimpleDateFormat class) and use their before(...) and after(...) methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!