0

I have the following query & PRCE regex from which i want to get table names.

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

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

Desired result student s subject s marks s I cant figure out how to extract from 1st match.

I'm trying to find & replace in sublime text editor.

Abdul Rehman
  • 1,662
  • 3
  • 22
  • 36
  • Try this one `\w+\s+[a-zA-Z][,\s*]` – Maraboc Nov 16 '15 at 15:19
  • @bobblebubble thanks for your input. That works with addition of match braces `()` but it selects invalid entry if the prefixed values is the last table name. https://regex101.com/r/mF9bY8/3 – Abdul Rehman Nov 16 '15 at 20:40
  • I removed answer because it won't be ever useful for anybody. [This was the last demo](https://regex101.com/r/jM3vY6/1) modify to your needs. – bobble bubble Nov 17 '15 at 11:15
  • 1
    Thanks for ur reply. i have got some good understanding from your help. I'll try to tweak that for my needs. Thanks :) – Abdul Rehman Nov 17 '15 at 11:17

2 Answers2

0

Try this: \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;
    }
Thiago Souza
  • 135
  • 2
  • 8
0

Using @bobblebubble solution which worked 90%, i added a bit more conditions to match my case. it works but its very aggressive and hangs the editor on large or multiple files. But i can live with what i have got. Hers the solution:

(?is)(?:\bFROM\b|\G(?!^))(?:[\s,]|#[^\s,]++)*(\b\K(?:\s*(?!WHERE|LEFT\b)\w+){4,})\b(?=.*?\bWHERE\b)
Abdul Rehman
  • 1,662
  • 3
  • 22
  • 36