• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

matcher find() problems

 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I think the first component matches U or C or nothing.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Alan.

I hadn't noticed this was a duplicated thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic