regular expression in java

C

Chen Yang

Hi,

In String class, we can use

boolean matches(String regex)

to deal with string data. But when I write:

String da = new String("function");
System.out.print(da.matches("\bfunction\b"));

And it returns false.

I think \b means a word boundary. So it should return true in this case. I
can't figure out why, plez help me, thanks in advance.
 
V

VisionSet

Chen Yang said:
Hi,

In String class, we can use

boolean matches(String regex)

to deal with string data. But when I write:

String da = new String("function");
System.out.print(da.matches("\bfunction\b"));

And it returns false.

I think \b means a word boundary. So it should return true in this case. I
can't figure out why, plez help me, thanks in advance.

try:

String da = new String("function");
System.out.print(da.matches("\\bfunction\\b"));


the 1st \ escapes so the 2nd can be passed to the regex engine
 
C

Chen Yang

Thanks a lot in the first place. It does work. But when I use

String da = new String("this is a function");
System.out.print(da.matches("\\bfunction\\b"));

It returns false again.

I am wondering how to implement it as in regular expression... I found an
article from google, and it says " It is important to remember that
String.matches() only returns true if the entire string can be matched." So
does it mean we can't use /\bblahblah\b/ as we use in awk or something like
that? Thanks again for your reply. :)

http://www.regular-expressions.info/java.html

Chen
 
V

VisionSet

Chen Yang said:
Thanks a lot in the first place. It does work. But when I use

String da = new String("this is a function");
System.out.print(da.matches("\\bfunction\\b"));

It returns false again.

I am wondering how to implement it as in regular expression... I found an
article from google, and it says " It is important to remember that
String.matches() only returns true if the entire string can be matched." So
does it mean we can't use /\bblahblah\b/ as we use in awk or something like
that? Thanks again for your reply. :)

Because that method matches the *entire* input against the regex. There are
other methods that search for a match. But that one can be forced to work
by putting int .* at either end ie accept anything before & after. ie
String da = new String("this is a function");
System.out.print(da.matches(".*\\bfunction\\b.*"));

look at java.util.regex.Matcher.find() for searching within strings
 
C

Chen Yang

Hi,

Thanks a lot! I totally understand now. So it means I can only use this
regular expression to compare with "the whole" string. Thanks again.

Chen
 
R

Roedy Green

String da = new String("function");
System.out.print(da.matches("\bfunction\b"));

And it returns false.

I think \b means a word boundary. So it should return true in this case. I

I would write that as:

String da = "function";

Matcher m = pattern.matcher( da );

System.out.println( m.matches() );

....

static Pattern pattern = Pattern.compile( "\\sfunction\\s" );
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top