Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Format a string to an array

Last post 06-30-2009, 6:56 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  06-30-2009, 5:27 PM 54456

    Format a string to an array

    I need to format a string:

    1.1, 1.22, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9,

     to an array:

    1.1, 1.22, 1.3,

    1.4, 1.5, 1.6,

    1.7, 1.8, 1.9,

     Find the third "," and replace it with ",\r" should. Just don't know how to do it.

    Anybody can help on this?

    Thanks! 

     

     

     

     

  •  06-30-2009, 6:56 PM 54458 in reply to 54456

    Re: Format a string to an array

    You don't say which regex variant you are using, nor the platform but the following will probaby work.

    Try a pattern of:

    ([^,]*,){3}

    and a replacement string of

    $0<cr><lf>

    where the "<cf><lf>" are whatever you need for your platform to create a new line - just be careful not to have the characters interpreted as text of you use something like "\r\n". Also, some regex variants use the "\0" form to reference a match group's contents.

    This assumes that there are no commas in any of the fields (i.e. you don't have fields such as:

    12, 34, "Hello, she said", 45.23, 543

    Also this will place a newline at the end of the last comma IF it is a multiple of 3 (as in your sample text) but NOT otherwise. 

    Susan 

View as RSS news feed in XML