• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

lexical search using Pattern and Matcher class

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Arthur, where are your pants? Check under this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic