my dog learned polymorphism
The moose likes Java in General and the fly likes Pattern.match() problem with $ in String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Pattern.match() problem with $ in String" Watch "Pattern.match() problem with $ in String" New topic
Author

Pattern.match() problem with $ in String

Amrit Tiwana
Greenhorn

Joined: Jun 03, 2004
Posts: 24
Hi,

I'm want to compare a a string against a regex. For this i m using
java.util.regex.Pattern.matches(regex, str). My problem is tht regex need not necessarily be a regex, it can be a string.
Now when I use argument like Pattern.matches("Sheet$", "Sheet$" ) it returns me false, as '$' means The end of a line(i also dont know where can I use it).

But I dont want to treat $ in string, as regex but as normal character.
please let me know the way to obtain true for input strings("Sheet$").
I tried Sheet\\p(Punct) as regex, but this will match with other strings too like Sheet* , Sheet! etc... but I want true for only exact match Sheet$.

Thanks
[ September 26, 2005: Message edited by: Amrit Tiwana ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16692
    
  19

Have you tried "Sheet\\$" ?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Matcher has a quoteReplacement method that escapes any special characters. If your input might or might not be a regex you'd have to know when to call it and when not to. Does that help?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
Originally posted by Stan James:
Matcher has a quoteReplacement method that escapes any special characters. If your input might or might not be a regex you'd have to know when to call it and when not to. Does that help?


Perfectly right. If you want the string input directly (as is) as your pattern without any regex escapes, just do this
StringBuffer sb= "\Q";
sb.append(input).append("\E");
String s= sb.toString();

that should do the job (I'm not sure if we need to doubly escape \Q and \E) If this doesn't work, the thing you should do is, replace "\Q" and "\E" with "\\Q" and "\\E"


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[Stan]: Matcher has a quoteReplacement method that escapes any special characters.

Matcher has a quoteReplacement method which escapes only \ and $ chars, because those are the only ones that have special significance for a replacement string (i.e. a string used as the second argument to String's replaceAll(), or the only argument to Matcher's replaceAll() or replaceFirst(), or the second argument to appendReplacement(). Replacement strings are a very specific context with special rules.

If you want to escape all special characters in a regex, there's \\Q and \\E as Ashkay Kiran said. Or you can use Pattern.quote(). Note that Pattern.quote() and Matcher.quoteReplacement() were added in JDK 5, while \\Q and \\E were available in 1.4.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Pattern.match() problem with $ in String
 
Similar Threads
regex question
about criteriaFind
[regex help]Find EXACT characters in a String.
Seemingly simple regex making my head hurt
Regular Expression