需要Python中正则表达式的指导



我需要帮助完成我当前的一项任务,其中我试图通过Python 仅从查询中选择表名

所以基本上假设一个查询看起来像这个

Create table a.dummy_table1
as
select a.dummycolumn1,a.dummycolumn2,a.dummycolumn3 from dual
Now i am passing this query into Python using STRINGIO and then reading only the strings where it starts with "a" and has "_" in it like below
table_list = set(re.findall(r'ba.w+', str(data)))
Here data is the dataframe in which i have parsed the query using StringIO
now in table_list i am getting the below output
a.dummy_table1
a.dummycolumn1
a.dummycolumn2

whereas the Expected output should have been like 
a.dummy_table1

<让我知道我们如何做到这一点,已经尝试了上面的正则表达式,但它不能正常工作>

如有任何帮助,我们将不胜感激

您当前的正则表达式字符串r"\ba。\w+";只匹配任何字符串:

  1. 以";a";("\ba"部分(
  2. 后面跟着一个句号("."部分(
  3. 后面跟一个或多个字母数字字符("\w+"部分(

如果我正确理解了您的问题,您希望从str(数据(中提取任何与此模式匹配的字符串片段:

  1. 以";a">
  2. 后面跟着一段时间
  3. 后跟一个或多个字母数字字符
  4. 后跟下划线
  5. 后跟一个或多个字母数字字符

因此,正则表达式应该具有"0"_\w+";添加到末尾以匹配标准4和5:

table_list = set(re.findall(r"ba.w+_w+", str(data)))

最新更新