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

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

    It'll probably be easier to search the string, grabbing each part, vs. split it.

    Reason being, you can have it split at the spaces before and after "will be". But, I can't think of any way to specify ignoring the space between inside a split.

    (not actual Java)

    string = "This is a string that \"will be\" highlighted when your 'regular expression' matches something.";
    
    regex = "\"(\\\"|(?!\\\").)+\"|[^ ]+"; // search for a quoted or non-spaced group
    final = new Array();
    
    while (string.length > 0) {
        string = string.trim();
        if (Regex(regex).test(string)) {
            final.push(Regex(regex).match(string)[0]);
            string = string.replace(regex, ""); // progress to next "word"
        }
    }
    

    Also, capturing single quotes could lead to issues:

    "Foo's Bar 'n Grill"
    
    //=>
    
    "Foo"
    "s Bar "
    "n"
    "Grill"
    
    0 讨论(0)
  • 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)
    0 讨论(0)
  • 2020-11-22 03:51

    1st one-liner using String.split()

    String s = "This is a string that \"will be\" highlighted when your 'regular expression' matches something.";
    String[] split = s.split( "(?<!(\"|').{0,255}) | (?!.*\\1.*)" );
    

    [This, is, a, string, that, "will be", highlighted, when, your, 'regular expression', matches, something.]

    don't split at the blank, if the blank is surrounded by single or double quotes
    split at the blank when the 255 characters to the left and all characters to the right of the blank are neither single nor double quotes

    adapted from original post (handles only double quotes)

    0 讨论(0)
提交回复
热议问题