Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By JosAH

Thread: regular expression issue

  1. #1
    flamant is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default regular expression issue

    Hi,
    I use the method String.matches(String regex) to find if a string matches the regex expression


    From my point of view the regular expression regex="[0-9]+" means a String that contains at least one figure between 0 and 9

    But when I debug "3.5".matches("[0-9]+") it returns false.

    So what is wrong ?

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: regular expression issue

    "[0-9]+" means a string that contains at least one figure between 0 and 9, and nothing else.

    If you wanted to match "3.5", you could do it a variety of ways. But if you're trying to match valid decimal numbers, you'll need to consider a few things. Do you want to allow leading and trailing zeros? Must there be a numeral before the decimal point? Et cetera.
    Get in the habit of using standard Java naming conventions!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default Re: regular expression issue

    The matches( ... ) method tries to match the entire String against the pattern. In your case it fails for obvious reasons. Use a Pattern object and a Matcher object instead and use the method(s) in the Matcher class to find matches anywhere in the String.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: regular expression issue

    From my point of view the regular expression regex="[0-9]+" means a String that contains at least one figure between 0 and 9
    Like any API the Java classes attempt to "do the right thing". But when the right thing fails to happen we have to check the API docs.

    String matches() says "An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str)". So we look at Pattern's matches() which sends us on our way to Matcher's matches(). (Basically the regex is compiled to a pattern from which a Matcher instance is created based on the string we are matching against). Finally! And we find "Attempts to match the entire region against the pattern".

    So what is wrong ?
    The problem is that the String method matches the entire string against the pattern.

    -----

    Reading those API docs is one way of approaching the likely next question. Or read about regular expressions in Oracle's Tutorial.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: regular expression issue

    Also at javaprogrammingforums.com

    Please, if you are going to start a discussion in multiple forums, post links letting everyone know where else the discussion might be taking place. Bear in mind, too, that many people won't spend time on questions that might well have been answered elsewhere.

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: regular expression issue

    pbrockway2, how can you tell when a question is cross-posted? Do you have a program that searches the two forums for duplicate titles?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: regular expression issue

    Like a number of people here I'm something of an addict - although not to the extent of some, who couldn't stop no matter what they might claim! A while back this forum's database went sour for a while and I ended up at jpf.com for a fix. I just stayed. The way you do.

    I draw attention to multiple posts as a service to others. (and not with any heat, or antagonism) Also to push my (minority!) view of forums as places of collegial discussion not just somewhere to obtain teh codez. Think a stoa in Athens, not the temple at Delphi.

    Of course there is software that quickly detects the antics of these fools, and some posts cry out for it. Consider Google, multiposters! And repent!
    kammce likes this.

  8. #8
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: regular expression issue

    Wow, that's pretty cool!

    Also, I'm not trying to make a joke here, I swear I'm just genuinely curious: Did somebody already register as pbrockway before you?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default Re: regular expression issue

    Quote Originally Posted by awinston View Post
    Wow, that's pretty cool!

    Also, I'm not trying to make a joke here, I swear I'm just genuinely curious: Did somebody already register as pbrockway before you?
    No, it's just marketing; pbrockway2 sounds much more mature than pbrockway1; that smells like there are still bugs in it ;-)

    kind regards,

    Jos
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Regular expression
    By garnaout in forum New To Java
    Replies: 4
    Last Post: 05-15-2012, 07:22 PM
  2. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 04:33 AM
  3. regular expression
    By ehsansad in forum New To Java
    Replies: 8
    Last Post: 03-18-2011, 03:23 PM
  4. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 04:55 AM
  5. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •