Results 1 to 3 of 3
  1. #1
    PeteClimbs is offline Member
    Join Date
    Apr 2011
    Location
    Florida
    Posts
    2
    Rep Power
    0

    Default How to use regex and split to solve a problem

    I have a string:
    "BIDU","Baidu, Inc","4/19/2011",147.28,45.89

    When I do a split(",") on the string, the comma inside the double quotes causes an extra unwanted split. Is there a regex pattern that would split the string only at commas which are not inside double quotes. I have no control over the input as it comes from a web service.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Regex cant count, so it cant check if there is an open quote :)

    But the String contains only other inner Strings "..." or a double value or?

    Java Code:
    		String s ="\"BIDU\",\"Baidu, Inc\",\"4/19/2011\",147.28,45.89";
    		Matcher m = Pattern.compile("\".*?\"|\\d+\\.\\d+").matcher(s);
    		while(m.find()){
    			System.out.println(m.group());
    		}
    -->
    "BIDU"
    "Baidu, Inc"
    "4/19/2011"
    147.28
    45.89

  3. #3
    PeteClimbs is offline Member
    Join Date
    Apr 2011
    Location
    Florida
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Regex and split
    By sunde887 in forum New To Java
    Replies: 2
    Last Post: 02-14-2011, 12:54 AM
  2. Regex problem
    By Nimyz in forum Advanced Java
    Replies: 4
    Last Post: 05-14-2010, 07:17 AM
  3. split() function problem.
    By robWhittle in forum New To Java
    Replies: 2
    Last Post: 04-09-2010, 01:34 PM
  4. split method problem
    By javanew in forum New To Java
    Replies: 1
    Last Post: 04-02-2010, 05:50 PM
  5. split problem
    By mac in forum New To Java
    Replies: 4
    Last Post: 01-02-2010, 04:46 AM

Tags for this Thread

Posting Permissions

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