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

Splitting String

Last post 03-08-2010, 7:06 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  03-05-2010, 9:06 AM 60403

    Splitting String

    First of all ... I am new to regex

    I need to split a comma-seperated given string. The problem for me is that the values themselves contain both quotes and comma characters.

    This is the given string:

    allTfs = 'normalTradesFilter,"Trade,Filter,with,commas",TradeFilterWithoutCommas,TradeFilter"with"Quotes,"Trade,Filter\"With,Both\""'

    These should be the results

    tf1 = 'normalTradesFilter'

    tf2 = 'Trade,Filter,with,commas'

    tf3 = 'TradeFilterWithoutCommas'

    tf4 = 'TradeFilter"with"Quotes'

    tf5 = 'Trade,Filter\"With,Both\"'

    How to specify the regex expression? Thanks for any help.

    best wishes, thomas

  •  03-05-2010, 11:09 AM 60409 in reply to 60403

    Re: Splitting String

    As requested in the Posting Guidelines, please specific the programming language/Application you are working with.

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  03-08-2010, 7:06 PM 60498 in reply to 60403

    Re: Splitting String

    Please be careful with your samples - the first item begins with a single quote but does not have one at the end (but your required output does); the second example uses double quote characters but the corresponding output shows single quotes.

    Also how are the single/double quote characters escaped with used within items that have the same quote characters? Is it always a leading "\" or are there other ways of showing this?

    Do you mean "split" or do you mean "match" as these are quite different operations for a regex? A "split" means you specify what lies between the items, where as a "match" means you specify the items themselves. In your case, a "split" would concentrate on the commas and determine if a comma is within a quoted strung or not, whereas a "match" would concentrate on what makes up a valid value and the fact that the are separated by commas is (almost) irrelevant - you could just as easily have tabs, spaces or semicolons as delimiters.

    Susan

View as RSS news feed in XML