• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex - string matching  RSS feed

 
deepak reads
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a string "menulink.roleId.13.display.1" and also a string "menulink.roleId.1.". When i use the


matcher.find() returns true.But i want to matching should return false as i want the "menulink.roleId.1." string to be taken as one. Are there any options to get whai i want ?
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,

Have you tried using matcher.matches() method?

I just check it with your code and it is giving me the desire results.

Let me know if this solve your problem.

Regards
Naveen
 
deepak reads
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks naveen. Actually i missed one thing. when i should give

This should give me true. When i try matches() it tries to find the exact match.If the pattern is a part of string, it returns false.

Finding no solutions in regex, i went for string.lastIndexOf. It works fine for my requirement.

Thx,
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem in the regex solution is that a '.' is a special character in regular expressions - it's a wildcard that can match any character (except possibly line terminators, depending on the usage mode). See here for more info. To avoid this behavior and treat the . as a literal ., there are several options:

Escape each . by prefacing with \\:

Use \\Q and \\E to quote the pattern literally:

Or use Pattern.quote() (available in JDK 1.5+) to quote literally:

[ July 11, 2006: Message edited by: Jim Yingst ]
 
deepak reads
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!