Regex split string preserving quotes

前端 未结 2 1540
走了就别回头了
走了就别回头了 2020-12-05 06:07

I need to split a string like the one below, based on space as the delimiter. But any space within a quote should be preserved.

research library \"not availa         


        
相关标签:
2条回答
  • 2020-12-05 06:27

    Here you go:

    C#:

    Regex.Matches(subject, @"([^\s]*""[^""]+""[^\s]*)|\w+")
    

    Regular expression:

    ([^\s]*\"[^\"]+\"[^\s]*)|\w+
    
    0 讨论(0)
  • 2020-12-05 06:30

    As long as there can be no escaped quoted inside quoted strings, the following should work:

    splitArray = Regex.Split(subjectString, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
    

    This regex splits on space characters only if they are preceded and followed by an even number of quotes.

    The regex without all those escaped quotes, explained:

    (?<=      # Assert that it's possible to match this before the current position (positive lookbehind):
     ^        # The start of the string
     [^"]*    # Any number of non-quote characters
     (?:      # Match the following group...
      "[^"]*  # a quote, followed by any number of non-quote characters
      "[^"]*  # the same
     )*       # ...zero or more times (so 0, 2, 4, ... quotes will match)
    )         # End of lookbehind assertion.
    [ ]       # Match a space
    (?=       # Assert that it's possible to match this after the current position (positive lookahead):
     (?:      # Match the following group...
      [^"]*"  # see above
      [^"]*"  # see above
     )*       # ...zero or more times.
     [^"]*    # Match any number of non-quote characters
     $        # Match the end of the string
    )         # End of lookahead assertion
    
    0 讨论(0)
提交回复
热议问题