Java Regular Expressions Help

L

Lee

Hi there,

I wonder if anyone can tell me how to set a boolean if I encounter a
word in a string (in upper, lower, or a combination of cases) e.g. I
want to set a boolean to true if either "hello", "HELLO", or "hElLo" is
found in "Hello there!".

So far, I have

Pattern p = Pattern.compile("HELLO");
Matcher m = p.matcher("HELLO there!");
myBoolean = m.lookingAt();

Can anyone tell me how to modify the above so that is will match true
for upper/lower case, and will return true even in the case:
" 4HELLO123"


Stumped!!!

Thanks,
Lee
 
M

Michael Borgwardt

Lee said:
I wonder if anyone can tell me how to set a boolean if I encounter a
word in a string (in upper, lower, or a combination of cases) e.g. I
want to set a boolean to true if either "hello", "HELLO", or "hElLo" is
found in "Hello there!".

So far, I have

Pattern p = Pattern.compile("HELLO");
Matcher m = p.matcher("HELLO there!");
myBoolean = m.lookingAt();

Can anyone tell me how to modify the above so that is will match true
for upper/lower case, and will return true even in the case:
" 4HELLO123"


Pattern p = Pattern.compile("HELLO", Pattern.CASE_INSENSITIVE);
 
T

Tor Iver Wilhelmsen

Lee said:
Can anyone tell me how to modify the above so that is will match true
for upper/lower case, and will return true even in the case:
" 4HELLO123"

To match a substring, use find() instead of lookingAt() or matches().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top