【问题标题】:Regex - Pull all strings, but with spaces if in between quotes (Preferably without quotes)正则表达式 - 拉出所有字符串,但如果引号之间有空格(最好不带引号)
【发布时间】:2017-10-01 14:24:01
【问题描述】:

所有,我正在尝试拉一个字符串集合/数组。我发现 Regex 可能是最好的方法。

我有一个这样格式化的字符串,略有不同

string1 "string 2" "string.3" string4 string5

我想像这样输出所有内容

output 1 = string1
output 2 = string 2 //without quotes preferably
output 3 = string.3 //without quotes preferably
output 4 = string4
output 5 = string5

我真的只需要正则表达式部分。

【问题讨论】:

    标签: regex quotes


    【解决方案1】:

    我在浏览器控制台 JavaScript 正则表达式中试过这个

    (\w+[\s\d.]+)

    'string1 "string 2" "string.3" string4 string5'.match(/(\w+[\s\d.]+)/gi)
    
    // output is
    ["string1 ", "string 2", "string.3", "string4 ", "string5"]
    

    【讨论】:

      【解决方案2】:

      取决于你将如何处理它,以及使用什么语言,这可能会得到你想要的(它适用于 javascript、C#):

      [^ "][^ ]*|"([^"]*)"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-29
        • 2010-09-13
        • 1970-01-01
        • 2016-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多