Results 1 to 4 of 4
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    642
    Rep Power
    3

    Default Regex Q - gotta be something obvious

    SSCCE:

    Java Code:
    public class SplitEscape {
    
    	public static void main(String[] args) {
    		String string = (char) 27 + "[1;32m";
    		String[] split = string.split("[^0-9]+");
    		System.out.printf("The resulting array has %d elements.\n", split.length);
    	}
    
    }
    The output is 3 elements, "", "1", and "32". I expected 2 elements, "1" and "32". What gives?
    Get in the habit of using standard Java naming conventions!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,271
    Blog Entries
    1
    Rep Power
    25

    Default Re: Regex Q - gotta be something obvious

    What is the motivation driving this? What is your ultimate goal with this code?

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    642
    Rep Power
    3

    Default Re: Regex Q - gotta be something obvious

    NM - found the answer on StackOverflow. This is just how it is when there's a leading delimiter.

    An inefficient but simple solution:

    Java Code:
    input.replaceFirst("^"+regex, "").split(regex);
    Get in the habit of using standard Java naming conventions!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,101
    Rep Power
    17

    Default Re: Regex Q - gotta be something obvious

    ANSI escape sequences, in this day and age?

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Regex help?
    By l3ane in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 06:04 AM
  2. This will have an obvious answer!
    By MarkusHendersonicus in forum New To Java
    Replies: 6
    Last Post: 12-24-2010, 12:49 AM
  3. Using Regex
    By ron87 in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 06:26 AM
  4. Please Help - Regex
    By BeeGee in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2010, 05:28 PM
  5. gotta loving this forum
    By Y. Progammer in forum Suggestions & Feedback
    Replies: 0
    Last Post: 02-20-2010, 10:28 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
  •