[英]Regular expression to extract lines between two strings in Javascript
I have text with SAS code in it. 我有带SAS代码的文本。 Essentially it is SQL query wrapped between
本质上是SQL查询之间
PROC SQL;
.
.
.
.
.
QUIT;
I am trying to extract SQL in between as a single match. 我试图提取SQL之间的单个匹配。
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 理想情况下,如果我想从文本文件中提取每个SQL查询
Any leads are well appreciated. 任何线索都很好。
You can add the s
modifier to your regex which allows the .
您可以将
s
修饰符添加到允许使用的正则表达式中.
character to also match newlines, and i
for matching case variation. 字符也要匹配换行符,而
i
则要匹配大小写变化。 Also use non-capturing groups for the prefix and suffix: 还可以使用非捕获组作为前缀和后缀:
/^(?:START\-OF\-FIELDS)(.*)(?:END\-OF\-FIELDS)$/mgsi
I've a doubt that this will solve your real problem to get SQL or not? 我怀疑这是否可以解决您真正的问题以获取SQL? , 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.