wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes use of pattern mathces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "use of pattern mathces" Watch "use of pattern mathces" New topic
Author

use of pattern mathces

Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
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

Joined: Jan 04, 2001
Posts: 245
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?
Peter Kristensson
Ranch Hand

Joined: Jul 02, 2001
Posts: 118
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

Joined: Jan 04, 2001
Posts: 245
Thanks Peter,
I tried replacing the "." with the pattern u have suggested but it does not work.
Am I going wrong anywhere else too?
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
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
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
"." - matches a single character
".+" - matches one or more characters
".*" - matches zero or more characters
Is this what you were trying to figure out?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Gemini Moses
Ranch Hand

Joined: Jan 04, 2001
Posts: 245
Dirk ,
You are right. this is what exactly I was looking for...
Thanks Jim for your help too.
I had missed that too...
 
Don't get me started about those stupid light bulbs.
 
subject: use of pattern mathces
 
Similar Threads
Is this an anti-pattern?
regex confusion
about a very BIG confusion in design pattern.
java,util.regex pains
Directory Error