File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes matcher find() problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "matcher find() problems" Watch "matcher find() problems" New topic
Author

matcher find() problems

Tyler Jordan
Ranch Hand

Joined: Nov 17, 2003
Posts: 70
Hi,
According to Sun, the find() method under matcher does the following:

"Attempts to find the next subsequence of the input sequence that matches the pattern."

So in human this means to me that find() returns true if a string CONTAINS a sequence that matches the regex. However, with my program, it only returns true if the string itself matches the regex. So with the following inputs:

REGEX = "^[U||C]J[A-Z]{5}[0-9]{3}$";

INPUT = "UJZEDGG111"; returns true
INPUT = "<**>UJZEDGG111"; returns false (but should return true)

Here is my full code:



Did I do something wrong here?

-Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Your first term in the regex looks for U or C at the beginning of the line, if I remember correctly. You will have to go through the regular expression part of the Java Tutorial expecially the bit about "boundary matchees" to check that ^ does mean start of line.

The <> bit moves U away from the start of the line.
Tyler Jordan
Ranch Hand

Joined: Nov 17, 2003
Posts: 70
Good call, the ^ and $ were it. Basically I was saying that all the line should contain is my regex. I removed those chars from my regex and it matches as expected now.

Thanks for the help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Actually, I think the first component matches U or C or nothing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

How does this thread differ from this one?

Since there is a bit more info in this one, I'm closing the other one, but in the future please UseOneThreadPerQuestion.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Originally posted by Campbell Ritchie:
Actually, I think the first component matches U or C or nothing.
Actually, it matches 'U' or 'C' or '|'. As I explained in the other thread, inside a character class, '|' loses its special meaning and just matches a '|'.

Come to think of it, I also explained about the '^' and '$' anchors in the other thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Thank you, Alan.

I hadn't noticed this was a duplicated thread.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: matcher find() problems
 
Similar Threads
How to mask string not conforming to a regular expression pattern
How to use OR in Regular Expressions?
Using java.util.regex.Pattern
Please help me check this regex
Regular expressions: Grouping