Regex for splitting a string using space when not surrounded by single or double quotes

后端 未结 15 2373
梦毁少年i
梦毁少年i 2020-11-22 03:15

I\'m new to regular expressions and would appreciate your help. I\'m trying to put together an expression that will split the example string using all spaces that are not s

15条回答
  •  春和景丽
    2020-11-22 03:45

    A couple hopefully helpful tweaks on Jan's accepted answer:

    (['"])((?:\\\1|.)+?)\1|([^\s"']+)
    
    • Allows escaped quotes within quoted strings
    • Avoids repeating the pattern for the single and double quote; this also simplifies adding more quoting symbols if needed (at the expense of one more capturing group)

提交回复
热议问题