M
M
marselabdullin2022-03-18 21:58:30
Regular Expressions
marselabdullin, 2022-03-18 21:58:30

How to get a line from a file skipping certain words with a regular expression?

There is a text file, you need to get a line from it - the first occurrence containing DmpKey, from which hashdiff is taken (in this case, DateStock_DmpKey marked in the text of the file)

SELECT 
  ds.DateStock_DmpKey,
  ds.Location_DmpKey,
  ds.Item_DmpKey,
  ds.Date_DmpKey,
  ds.LoadDTM,
  ds.RecSrc,
  ds.SnapshotDT
FROM (
  SELECT
    HASH('DateStock*' || ds.Location_BizKey || '*' || ds.Item_BizKey || '*' || ds.Date_DmpKey) AS DateStock_DmpKey(это он) ,
    HASH(ds.Location_BizKey) AS Location_DmpKey,
    HASH(ds.Item_BizKey) AS Item_DmpKey,
    ds.Date_DmpKey,
    '{{ macros.datetime.now() }}' AS LoadDTM,
    'naCitilink*Irk' AS RecSrc,
    '{{ ds }}' AS SnapshotDT
  FROM (
    SELECT 
      'Location*naCitilink*Irk*' ||  CAST(stg."Location Code" AS varchar(40))  AS Location_BizKey,
      'Item*naCitilink*' || CAST(stg."Item no_" AS varchar(100))                AS Item_BizKey,
      to_char(DateP, 'YYYYMMDD')::int                                           AS Date_DmpKey
    FROM Stage.STG_Ctl_IRK_Date_Stock stg
    WHERE 
      -- catchup_snapshotdt_start
      stg.SnapshotDT = '{{ ds }}'
      -- catchup_snapshotdt_end
    LIMIT 1 OVER (PARTITION BY stg.DateP, stg."Location Code", stg."Item no_" ORDER BY stg.LoadDTM DESC)
  ) AS ds
) AS ds

LEFT JOIN RawVault.RV_Lnk_DateStock tgt
  ON tgt.DateStock_DmpKey = ds.DateStock_DmpKey
  AND tgt.RecSrc = ds.RecSrc
  
WHERE tgt.DateStock_DmpKey IS NULL;


This regex works - (?<=HASH\(.*\)\s+AS\s+).+_DmpKey, but it throws an error
look-behind requires fixed-width pattern
need to get rid of an indefinite number of characters in an expression with ?<=

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2022-03-18
@marselabdullin

import re

text = "HASH('DateStock*' || ds.Location_BizKey || '*' || ds.Item_BizKey || '*' || ds.Date_DmpKey) AS DateStock_DmpKey(это он) "

m = re.search('HASH\(.*\)\s+AS\s+(\w+_DmpKey)', text)

print(m.group(1))

M
marselabdullin, 2022-03-20
@marselabdullin

dodo512 , this expression takes the entire string in the text variable, and I only need DateStock_DmpKey, that is, rely on hash(...) as when searching and not take it into the final result (there is a ?<= parameter for this, but in it can't send an unlimited number of characters)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question