簡體   English   中英

使用 python 在多個字符串中查找並計算 substring 的實例

[英]Find and count instance of substring within multiple strings using python

我在 python 中有一個帶有多個子查詢的 sql 查詢。 所以設置是一個較大字符串中的多個子字符串。 我想檢查子字符串中字符串的實例數。 比我所看到的更多參與並感謝您的幫助。

像這樣設置 -

qry = ''' 
with 
qry_1 as ( 
   SELECT ID, 
          NAME
   FROM   ( ... other code...
),
qry_2 as ( 
    SELECT coalesce (table1.ID, table2.ID) as ID,
           NAME
   FROM (...other code...
),
qry_3 as (
     SELECT id.WEATHER AS WEATHER_MORN,
            ROW_NUMBER() OVER(PARTITION BY id.SUN
                ORDER BY id.TIME) AS SUN_TIME,
            id.RAIN,
            id.MIST
   FROM (...other code..
)
'''

我想計算qry_1, qry_2, qry_3內的ID實例。

我認為會利用re.findall和 substring 搜索?

re.findall(r'as \( select (.+?) from \(',qry)

然后在其中查找和計算ID實例? output 是 2。但我不確定如何......

您可以拆分 CTE 查詢,然后在子查詢的截斷版本上使用re.findall

qry = ''' 
with 
qry_1 as ( 
  SELECT ID, 
      NAME
  FROM   ( ... other code...
),
qry_2 as ( 
  SELECT coalesce (table1.ID, table2.ID) as ID,
       NAME
FROM (...other code...
),
qry_3 as (
  SELECT WEATHER
FROM (...other code..
)
'''

def get_cols(s):
   [cte_name] = re.findall('^\w+(?=\sas)|(?<=with\s)\w+(?=\sas)', s)
   cols = re.findall('(?<=as\s)[\w\.]+|(?<=SELECT\s)[\w\.]+|(?<=,\s)[\w\.]+', s)
   return [cte_name, cols]

#dictionary with the cte name as the key, and the columns as the values
v = dict(get_cols(re.sub('coalesce\s\(.+\)|[\s\n]+', ' ', i)) for i in re.split('(?<=\)),(?:\s+)*\n', qry))
#filter the dictionary above to only include desired column names
r = {a:k if (k:=[i for i in b if i in {'NAME', 'ID'}]) else None for a, b in v.items()}

Output:

{'qry_1': ['ID', 'NAME'], 'qry_2': ['ID', 'NAME'], 'qry_3': None}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM