Results 1 to 4 of 4

Thread: regrex

  1. #1
    CaptainPlanet is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default regrex

    I'm trying to use regrex to separate int values from a string. Here is what I have.


    Scanner input = new Scanner(System.in);
    String date = input.next();

    Pattern p = Pattern.compile("-?\\d+");
    Matcher ma = p.matcher(date);
    ma.find();

    while (ma.find())
    {
    System.out.println(ma.group());
    }

    It works ok...

    I want to take the digits from a date format 02/03/04 or 3/5/2008

    My output is
    03
    04

    and
    5
    2008

    Could someone tell me why the first set is being skipped?

    Thanks!

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

    Default Re: regrex

    Java Code:
    ma.find();
    
    while (ma.find()) 
    {
        System.out.println(ma.group());
    }
    Notice you are calling ma.find() twice before you ever call System.out.println();

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: regrex

    And the term is regex or RegEx or Regular Expressions. Not regrex.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: regrex

    Quote Originally Posted by DarrylBurke View Post
    And the term is regex or RegEx or Regular Expressions. Not regrex.
    I kind of pictured in my mind's eye Scooby Doo doing a bit of Java programming.

Posting Permissions

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