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

    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,316
    Blog Entries
    1
    Rep Power
    26

    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
    1,060
    Rep Power
    6

    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 offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Regex Q - gotta be something obvious

    ANSI escape sequences, in this day and age?

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

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
  •