繁体   English   中英

正则表达式字符串,其中空格是可选的,修剪后仅跟 (

[英]Regex string where space is optional and trim is followed only by (

可以说我有

string abc = "and TRIM$ ( 000trim) and (trim000) and  (abctrim) and Trim(trima) and  TRIM (  abc  ) and trim( A) and 0trim"

我只希望TRIM()函数更改为RTRIM()并且希望模式忽略空格(如果有的话)并考虑(TRIM

我在用

Regex.Replace(abc, "(?i)([^A-Za-z0-9])TRIM([^A-Za-z0-9][(])", "$1RTRIM(");

上面代码的结果是

“和RTRIM( (000trim)和(trim000)和(abctrim)和Trim (trimaa)和RTRIM(abc)trim(A)和0trim”;

我想得到输出

“和TRIM$ (000trim)和(trim000)和(abctrim)和RTRIM (trimaa)和RTRIM(abc)RTRIM(A)和0trim”;

我怎样才能达到预期的输出?

用这个 :

Regex.Replace(text, @"\bTRIM(?=\s*\()", "RTRIM", RegexOptions.IgnoreCase)

正则表达式匹配trim被立刻后跟0或多个空格(包括选项卡)和一个开括号(RegexOptions.IgnoreCase是不言自明...

编辑:添加(?<![$%])以使其忽略带有前导%$ trim ,如下所示:

Regex.Replace(text, @"\b(?<![$%])TRIM(?=\s*\()", "RTRIM", RegexOptions.IgnoreCase)

你可以做这样的替换:

var input = @"and TRIM$ ( 000trim) and (trim000) and  (abctrim) and Trim(trima) and  TRIM (  abc  ) and trim( A) and 0trim";
var pattern = @"(?i)\btrim(?:\s+|)(\([^)]+\))";
string res = Regex.Replace(input, pattern, "RTRIM$1");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM