Java Regular expression to find out all the columns of a particular table using table alias

Anna :

In the below query , I need to search for all the columns of the table1 using alias T_1

select T_1.name, T_1.age, T_2.dept from table1 T_1, table2 T_2 where T_1.id=T_2.id

Answer: name, age, id

I am using the below expression, in that i want to end the search when a special character occur. In the below expression I am ending the search when there is "=", but I want to generalize when there is a non aplpha numeric character.

"(?i)\\b" + T_1 + "\\." + "[a-zA-Z]*([^\\=]+)"

Regular expression should work for below query also, select T_1.name, T_1.age from table1 where T_1.id>100

Wiktor Stribiżew :

You may use

(?i)\bT_1\.(\w+)

See the regex demo. Note the \w+ does not match "special" chars, only letters, digits or underscores. If underscores are not allowed, replace \w+ with [^\W_]+, or \p{Alnum} and append \b word boundary:

(?i)\bT_1\.([^\W_]+)\b

Details

  • (?i) - case insenstivie mode on
  • \b - a word boundary
  • T_1\. - T_1. substring
  • (\w+) - Group 1: one or more letters, digits or _. To exclude _ and only match letters or digits, use ([^\W_]+) or (\p{Alnum}+) and append \b word boundary.

Java usage:

String regex1 = "(?i)\\bT_1\\.(\\w+)";
String regex2 = "(?i)\\bT_1\\.([^\\W_]+)\\b";

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170314&siteId=1