Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: \d+ does not work with split giving same result as just \d

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default \d+ does not work with split giving same result as just \d

    Hello.

    \d+ does not work with split giving same result as just \d

    System.out.println(Arrays.toString("1+2+13".split("((?=(\\d+))|(?=\\+))")));

    gives me [, 1, +, 2, +, 1, 3]

    What do I need to do if I want: [1, +, 2, +, 13] instead?

    Thanks in advance


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: \d+ does not work with split giving same result as just \d

    StackOverflow: How to split a string but also keep the delimiters

    The accepted answer may be what you're looking for, applied here you just use + instead of ; as the "delimiter".

    System.out.println(Arrays.toString("1+2+13".split("((?<=(\\+))|(?=\\+))")));

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: \d+ does not work with split giving same result as just \d

    Thanks. But why doesn't \d+ work?

    Edit:

    Why
    System.out.println(Arrays.toString("3+4-(2*3)^16".split("(?=(\\^))|(<=(\\^))|(?=(\\*))|(<=(\\*))|(?=(\\/))|(<=(\\/))|(?=(\\+))|(<=(\\+))|(?=(-))|(<=(-))|(?=(\\())|(<=(\\())|(?=(\\)))|(<=(\\)))")));

    gives me [3, +4, -, (2, *3, ), ^16] and not [3, +, 4, -, (2, *, 3, ), ^, 16]

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: \d+ does not work with split giving same result as just \d

    Why \d+ doesn't work:

    You're using a look-ahead match, which implicitly means the actual match group is the "space" between characters, i.e. match.start() = match.end(). Since you're matching at least one digit, the starting edge is always the same. The split algorithm can't tell the difference between the two so you get the same result.

    That's a rather long and convoluted regex pattern, I'm not quite sure where to begin with debugging it.

    A better solution is to utilize character classes which match all the appropriate operators.

    System.out.println(Arrays.toString("3+4-(2*3)^16".split("(?<=([+\\-*/\\^\\(\\)]))|(?=([+\\-*/\\^\\(\\)]))")));

    Depending on how complex you're planning to get with your tokenizer, it may be better to use a different technique. Rather than trying to split everything with one regex, try parsing the tokens as the come. This way each regex stays simple, is easy to modify, and is specific to what token you're trying to parse.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: \d+ does not work with split giving same result as just \d

    thaks.

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. split a paragraph
    By deepikamsc2010 in forum Collections and Generics
    Replies: 1
    Last Post: July 3rd, 2012, 09:25 AM
  3. Using char at to split up a interger
    By mooncowtime in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 26th, 2011, 08:55 AM
  4. Java split problem..
    By arch in forum Java SE APIs
    Replies: 5
    Last Post: August 11th, 2011, 07:48 AM
  5. Help me split and then join a file
    By babbupandey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 13th, 2010, 12:20 PM