Search code examples
phpshellawksed

How to get specific prefix content matched which inside whitespace using regular expression?


I have some normal SQL like below:

select dt from a.b.c where dt = '20210808' limit 10

So, now I want to get the table name that matches a specific prefix between the from and the where. If the prefix specified is 'a.', then the 'a.b.c' is matched and gets the table name 'c'.

In general return the last string separated by a dot if the prefix is matched. How to match it in regex? For now, I have the idea of matching starting whitespace + prefix string + ending whitespace, But I didn't write it.

Here is my PHP test code:

$sql = "select dt from a.b.c where dt = '20210808' limit 10";
$tmp_table_name_parten = '/\.([^.]*$)/';
preg_match_all($tmp_table_name_parten, $sql, $match);

But the result is:

result:[[".c where dt = '20210808' limit 10"],["c where dt = '20210808' limit 10"]]

Not my expectations, I want to get 'c'.


Solution

  • does the regular expression is different between various environments like SHELL、 PHP、JAVA, etc?

    Yes, they do differ. Term flavour is used to denote particular version. Examples are

    • POSIX Basic Regular Expression (BRE)
    • POSIX Extended Regular Expression (ERE)
    • Perl Compatible Regular Expressions (PCRE)

    If you are interested in flavour used in particular programming language please refer to Regular Expression Engine Comparison Chart