Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using regex question  RSS feed

 
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 ]
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Matcher.matches
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example code

 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!