Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using regex question

 
Paro Karisian
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use regex to match against patterns I set in an XML schema. The problem is that Matcher.find() returns a match even if it's only a partial match. For example, here is a pattern for zip code:

\d{5}(-\d{4})?

I only want it to return true if the ENTIRE input string matches the pattern.

So, "55555" or "55555-2222" should return true, but "55555-23" or "523121" returns false. Matcher.find() will always find a match for any of these values. What is the best way to make it return true only in the case that the entire input string matches?
[ October 14, 2004: Message edited by: Paro Karisian ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Matcher.matches
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example code

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic