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

后端 未结 15 2151
梦毁少年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:43

    If you are using c#, you can use

    string input= "This is a string that \"will be\" highlighted when your 'regular expression' matches ";
    
    List list1 = 
                    Regex.Matches(input, @"(?\w+)|\""(?[\w\s]*)""|'(?[\w\s]*)'|<(?[\w\s]*)>").Cast().Select(m => m.Groups["match"].Value).ToList();
    
    foreach(var v in list1)
       Console.WriteLine(v);
    

    I have specifically added "|<(?[\w\s]*)>" to highlight that you can specify any char to group phrases. (In this case I am using < > to group.

    Output is :

    This
    is
    a
    string
    that
    will be
    highlighted
    when
    your
    regular expression 
    matches
    something random
    

提交回复
热议问题