【问题标题】:regex get comma seperated values between two words正则表达式获取两个单词之间的逗号分隔值
【发布时间】:2015-11-16 13:43:26
【问题描述】:

我有以下查询和 PRCE 正则表达式,我想从中获取表名。

 FROM   student s, #prefix#.sometable, subject s, marks s   WHERE  ...

(?<=\sfrom)\s+\K(\w*)(?=\s+where)

想要的结果 student s subject s marks s 我不知道如何从第一场比赛中提取。

我正在尝试在 sublime 文本编辑器中查找和替换。

【问题讨论】:

  • 试试这个\w+\s+[a-zA-Z][,\s*]
  • @bobblebubble 感谢您的意见。这适用于添加匹配大括号(),但如果前缀值是最后一个表名,它将选择无效条目。 regex101.com/r/mF9bY8/3
  • 我删除了答案,因为它对任何人都没有用。 This was the last demo根据您的需要修改。
  • 感谢您的回复。我从你的帮助中得到了一些很好的理解。我会尝试根据我的需要进行调整。谢谢:)

标签: regex sublimetext3 pcre


【解决方案1】:

试试这个:\s+(\w*\s)*s

    pcre *myregexp;
    const char *error;
    int erroroffset;
    myregexp = pcre_compile("\\s+(\\w*\\s)*s", PCRE_CASELESS | PCRE_EXTENDED | PCRE_MULTILINE | PCRE_DUPNAMES | PCRE_UTF8, &error, &erroroffset, NULL);
    if (myregexp) {
        int offsets[2*3]; // (max_capturing_groups+1)*3
        int offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, 0, offsets, 2*3);
        if (offsetcount > 0) {
            pcre_get_substring(subject, &offsets, offsetcount, 1, &result);
            // group offset = offsets[1*2];
            // group length = offsets[1*2+1] - offsets[1*2];
        } else {
            result = NULL;
        } 
    } else {
        // Syntax error in the regular expression at erroroffset
        result = NULL;
    }

【讨论】:

  • 他想要一个 sublime 的正则表达式!!
【解决方案2】:

使用有效率为 90% 的 @bobblebubble 解决方案,我添加了更多条件来匹配我的情况。它可以工作,但非常具有侵略性,并且会将编辑器挂在大文件或多个文件上。但我可以忍受我所拥有的。她的解决方案:

(?is)(?:\bFROM\b|\G(?!^))(?:[\s,]|#[^\s,]++)*(\b\K(?:\s*(?!WHERE|LEFT\b)\w+){4,})\b(?=.*?\bWHERE\b)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多