如何找到最后一次出现的','在使用RegEx的c#中的字符串中

时间:2015-03-04 11:09:12

标签: c# regex

如何找到最后一次出现的','在使用RegEx

的c#中的字符串中

我试过下面的代码

string ss = Regex.Replace(strQuery, @"/\,(?<firstMatch>.*)=case(?<secondMatch>.*end\b)", @" case ${secondMatch} ""${firstMatch}"" ", RegexOptions.IgnoreCase);

输入

 SELECT  * FROM (select  TOP 5 ROW_NUMBER() OVER (ORDER BY Region_desc) as ROW_NUM,* from (select *, ROW_NUMBER() over (order by Region_desc desc) as gridviewcount from( SELECT BO_REGION_MASTER.Region_ID,BO_REGION_MASTER.Region_CODE,BO_REGION_MASTER.Region_DESC,STATUS=CASE WHEN BO_REGION_MASTER.STATUS='A' THEN 'ACTIVE' WHEN BO_REGION_MASTER.STATUS='I' THEN 'INACTIVE' ELSE '' END,

我正在获得输出

SELECT * FROM (select TOP 5 ROW_NUMBER() OVER (ORDER BY Region_desc) as ROW_NUM,* from (select *, ROW_NUMBER() over (order by Region_desc desc) as gridviewcount from( SELECT BO_REGION_MASTER.Region_ID,BO_REGION_MASTER.Region_CODE,BO_REGION_MASTER.Region_DESC,STATUS=CASE WHEN BO_REGION_MASTER.STATUS='A' THEN 'ACTIVE' WHEN BO_REGION_MASTER.STATUS='I' THEN 'INACTIVE' ELSE '' END,

但我需要输出如下

 SELECT  * FROM (select  TOP 5 ROW_NUMBER() OVER (ORDER BY Region_desc) as ROW_NUM,* from (select *, ROW_NUMBER() over (order by Region_desc desc) as gridviewcount from( SELECT BO_REGION_MASTER.Region_ID,BO_REGION_MASTER.Region_CODE,BO_REGION_MASTER.Region_DESC,CASE WHEN BO_REGION_MASTER.STATUS='A' THEN 'ACTIVE' WHEN BO_REGION_MASTER.STATUS='I' THEN 'INACTIVE' ELSE '' END "STATUS",

1 个答案:

答案 0 :(得分:0)

使用以下正则表达式,然后将匹配替换为$2 $1,

(?i).*,([^=]*)=(CASE.*),

OR

(?i).*,([^=]*)=(CASE.*end)\b

如果要进行不区分大小写的匹配,请在开头添加不区分大小写的修饰符(?i)

DEMO

string ss = Regex.Replace(strQuery, @".*,([^=]*)=(CASE.*end)\b",  "$2 $1", RegexOptions.IgnoreCase);

DEMO

<强>更新

(?i)(.*,)([^=]*)=(CASE.*end)\b

替换字符串:

$1$3 "$2"

DEMO