我需要正则表达式替换SQL语句中的字符

本文关键字:语句 字符 SQL 替换 正则表达式 | 更新日期: 2023-09-27 18:11:55

我正在尝试匹配这个SQL语句的BETWEEN范围,如下所示:

( WAEXD8 BETWEEN &WAEXD8 AND &WAEXD8 ) 

上面的SQL语句与DBS数据源一起工作,虽然通过ODBC驱动程序对该DB连接类型的iSeries连接有效,但对ADO.NET不起作用。ASP。NET ADO连接需要将BETWEEN范围替换为以下语法

date(digits(FIELDNAME) CONCAT '000000') <= current date and date(digits(FIELDNAME) CONCAT '000000') >= current date 

WHERE FIELDNAME是在原始SQL语句中找到的WAEXD8列…

我已经尝试过了,但到目前为止还没有找到一个真正的解决方案…

再一次,我希望得到一个ASP。NET c#正则表达式将原始SQL查询匹配/替换为替换字符串,同时保留原始列名。这可能吗?模式是什么,或者模式的例子是什么,这样我才能这么做?

可能的其他解决方案:

匹配(任何数量的带有BETWEEN的空格并且具有HTML实体&amp; with)

交货。: ([(])|([BETWEEN])*([0-9A-Za-z])*([)])

我需要正则表达式替换SQL语句中的字符

我不太确定你想要什么,但这里有一些例子应该涵盖你:

替换所有包含&amp;和相同FIELDNAME的BETWEEN语句

将匹配以下内容:

  • ( WAEXD8 BETWEEN &amp;WAEXD8 AND &amp;WAEXD8 )
  • (WAEXD8 BETWEEN &amp;WAEXD8 AND &amp;WAEXD8)
  • (WAEXD8 BETWEEN &amp;WAEXD8 AND &amp;WAEXD8 )
  • ( WAEXD8 BETWEEN &amp;WAEXD8 AND &amp;WAEXD8 )
  • ( FIELDEXAMPLE1 BETWEEN &amp;FIELDEXAMPLE1 AND &amp;FIELDEXAMPLE1 )

不匹配:

  • ( WAEXD8 BETWEEN WAEXD8 AND WAEXD8 )
  • ( WAEXD8 BETWEEN 0 AND 9000 )
  • ( WAEXD8 BETWEEN &amp;WAEXD8&A;NY1 AND &amp;WAEXD&8ANY2 )
  • ( FIELDEXAMPLE1 BETWEEN &amp;FIELDEXAMPLE2 AND &amp;FIELDEXAMPLE2 )


Regex regexObj = new Regex(@"'('s*('w+)'s+BETWEEN's+&amp ;'1's+AND's+&amp ;'1's*')");
resultString = regexObj.Replace(subjectString, "(date(digits($1) CONCAT '000000') <= current date and date(digits($1) CONCAT '000000') >= current date)");

或者,如果你知道字段名是AMAZINGFIELDNAME,那么:

 
Regex regexObj = new Regex(@"'('s*(AMAZINGFIELDNAME)'s+BETWEEN's+&amp ;'1's+AND's+&amp ;'1's*')");
resultString = regexObj.Replace(subjectString, "(date(digits($1) CONCAT '000000') <= current date and date(digits($1) CONCAT '000000') >= current date)");

用字符"&"answers";"

将匹配以下内容:

  • ( WAEXD8 BETWEEN &amp;WAEXD8 AND &amp;WAEXD8 )
  • ( WAEXD8 BETWEEN &amp;WAEXD8&A;NY1 AND &amp;WAEXD&8ANY2 )
  • ( FIELDEXAMPLE1 BETWEEN &amp;FIELDEXAMPLE2 AND &amp;FIELDEXAMPLE2 )

<>以前 Regex regexObj = new Regex(@"'('s*('w+)'s+BETWEEN's+['w&;]+'s+AND's+['w&;]+'s*')");resultString = regexObj.Replace(subjectString, "(date(digits($1) CONCAT '000000') <= current date and date(digits($1) CONCAT '000000') >= current date)");