• 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

use of pattern mathces

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to have
if (a.matches(%+"b"+%))
..
....
end if;
where a.matches (%+"b"+ %)
means I want to see if b is in a.
I am trying to find equavalent of ,
Where a like '%b%' from oracle in Java.
Can someone help me?
I tried ,
Pattern p = Pattern.compile("."+ "b" + ".");
Matcher m = p.matcher(rs.getString(l));
boolean b = m.matches();

but it does not work the way i want.
Where am I going wrong?
Thanks !!
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After taking a look at the jovadoc for this new class I tried the thing which i wrote above,
Pattern p = Pattern.compile("."+ "b" + ".");
Matcher m = p.matcher(rs.getString(l));
boolean b = m.matches();
Can u please help me build the pattern.
I mean as I uderstand this "." is not serving the purpose I want.
If I am not wrong, that is the only place where I am making some mistake. Am I right?
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I oogled through the docs and found that the '.' only matches one character whereas the % in oracle matches any character combination. Try using "\\Ab\\z" where \A is beginning of input and \z is end of input.
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter,
I tried replacing the "." with the pattern u have suggested but it does not work.
Am I going wrong anywhere else too?
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't used 1.4 regular expressions myself yet, but I seem to recall they are based on perl regular expressions. So to get the same as SQL's '%b%', you should have a pattern something like:
"^[^b]+b[^b]+"
Which is:
^ - anchor to front of string
[^b]+ - one or more non-'b' characters
b - a literal 'b'
[^b]+ - one or more non-'b' characters
Or something like that
Hope this helps, cheers Neil
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"." - matches a single character
".+" - matches one or more characters
".*" - matches zero or more characters
Is this what you were trying to figure out?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other elements that you might find useful:
"(.){0,1}" - matches zero or one characters
"(.){1,10}" - matches one or ten characters
"(.){3,}" - matches 3 or more characters
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a nice simple solution that's worked since JDK 1.0:

Or if you want to use regular expressions:

[Previous error corrected.]
[ August 13, 2002: Message edited by: Jim Yingst ]
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dirk ,
You are right. this is what exactly I was looking for...
Thanks Jim for your help too.
I had missed that too...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic