• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for regex matches() at the end of a string  RSS feed

 
Dale Preston
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a regular expression that I created and tested on some regex site, http://www.regular-expressions.info/javascriptexample.html. It worked on the test site but I can't get it to work in Java.

The code is:



This test always fails. I cannot figure out what is wrong with it. In fact, I can't get any match no matter what regex I use.
 
Dale Preston
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works:



No matter what I put in the parameter to .matches() I do not get a match. Is this a bug in Java 6?

Thanks,

Dale
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dale Preston wrote:


No matter what I put in the parameter to .matches() I do not get a match. Is this a bug in Java 6?



The matches() method only succeeds when the regex matches the whole string. It doesn't support partial matches -- as that is what the find() method is for.

And this is how it worked -- since regular expressions were added to Java.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dang it...i was just about there.

In other words, your pattern says the match must be the start of the string, followed by "_CAL" following by one or more digits, followed by the end of line.

Since your string s does not begin with "_CAL", it does not match.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!