0

I have text with SAS code in it. Essentially it is SQL query wrapped between

    PROC SQL;
    .
    .
    .
    .
    .
    QUIT;

I am trying to extract SQL in between as a single match.

This is how my text file looks like

***logic 1***
proc sql;
 create table Combined as
  select t1.name, t2.units
  from mdweop.candy_customers_azim_056 as t1
  inner join mdweop.candy_sales_history_set  t2 on (t1.custid = t2.ORIGTN_ACCT_NO);
quit;

***logic 2***
PROC SQL;
 CREATE TABLE COMBINED AS
  SELECT T1.RPOG_HMDA_CODES, T2.RSN_DECL1_C
  FROM MDWEOP.CANDY_CUSTOMERS AS T1
  INNER JOIN MDWEOP.CANDY_SALES_HISTORY AS T2
    ON (T1.CUSTID = T2.ACA_MISC_Z03HMDA_BV);
QUIT;

***logic 3***
PROC SQL;
 CREATE TABLE COMBINED AS
  SELECT T1.RRG_PRPS_CODES, T2.RSN_DECL1_C
  FROM MDWEOP.CANDY_CUSTOMERS AS T1
  INNER JOIN MDWEOP.CANDY_SALES_HISTORY AS T2
    ON (T1.CUSTID = T2.ACA_MISC_Z03HMDA_BV);
QUIT;

I tried this regex but its finding each line as single individual match.

Ideally if i want to extract each SQL query from the text file

Any leads are well appreciated.

2 Answers 2

2

You can add the s modifier to your regex which allows the . character to also match newlines, and i for matching case variation. Also use non-capturing groups for the prefix and suffix:

/^(?:START\-OF\-FIELDS)(.*)(?:END\-OF\-FIELDS)$/mgsi

See it in action here

Sign up to request clarification or add additional context in comments.

Comments

1

I've a doubt that this will solve your real problem to get SQL or not?, But I've posted this answer as per your regex demo.

const regex = /START-OF-FIELDS[\r\n]+(.*)[\n\r]+(.*)[\n\r](.*)[\n\r]END-OF-FIELDS/img;
const str = `START-OF-FIELDS
Line A
Line B
Line C
END-OF-FIELDS`;
let m;
const sql_lines = []
while ((m = regex.exec(str)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }

  // The result can be accessed through the `m`-variable.
  m.forEach((match, groupIndex) => {
    if (groupIndex > 0) {
      sql_lines.push(match);
    }
  });
}

console.log(sql_lines)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.