• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

matcher find() problems

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I think the first component matches U or C or nothing.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Alan.

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