Lets say I have
可以说我有
string abc = "and TRIM$ ( 000trim) and (trim000) and (abctrim) and Trim(trima) and TRIM ( abc ) and trim( A) and 0trim"
I want only the TRIM()
Function to change to RTRIM()
and would want the pattern to ignore the spaces if there are any and consider (
after the TRIM
我只希望TRIM()函数更改为RTRIM()并希望模式忽略空格(如果有)并考虑(在TRIM之后)
I am using
我在用
Regex.Replace(abc, "(?i)([^A-Za-z0-9])TRIM([^A-Za-z0-9][(])", "$1RTRIM(");
and the result for above code is
以上代码的结果是
"and RTRIM( ( 000trim) and (trim000) and (abctrim) and Trim(trimaa) and RTRIM( abc ) and trim( A) and 0trim";
“和RTRIM((000trim)和(trim000)和(abctrim)和Trim(trimaa)和RTRIM(abc)和trim(A)和0trim”;
I would want to get output as
我希望得到输出
"and TRIM$ ( 000trim) and (trim000) and (abctrim) and RTRIM(trimaa) and RTRIM ( abc ) and RTRIM ( A) and 0trim";
“和TRIM $(000trim)和(trim000)和(abctrim)和RTRIM(trimaa)和RTRIM(abc)和RTRIM(A)和0trim”;
How can I achieve desired output?
如何实现所需的输出?
2 个解决方案
#1
1
Use this :
用这个 :
Regex.Replace(text, @"\bTRIM(?=\s*\()", "RTRIM", RegexOptions.IgnoreCase)
The regex matches trim
that is immediatly followed by 0 or more spaces(including tabs) and a opening parenthesis (
. RegexOptions.IgnoreCase
is self-explanatory...
正则表达式匹配修剪,紧接着是0或更多空格(包括制表符)和一个左括号(.RegexOptions.IgnoreCase是不言自明的...
EDIT: add a (?<![$%])
to make it ignore trim
with leading %
or $
like this :
编辑:添加一个(?<![$%])使其忽略修剪前导%或$像这样:
Regex.Replace(text, @"\b(?<![$%])TRIM(?=\s*\()", "RTRIM", RegexOptions.IgnoreCase)
#2
0
You could do a replace like this:
你可以做这样的替换:
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");
#1
1
Use this :
用这个 :
Regex.Replace(text, @"\bTRIM(?=\s*\()", "RTRIM", RegexOptions.IgnoreCase)
The regex matches trim
that is immediatly followed by 0 or more spaces(including tabs) and a opening parenthesis (
. RegexOptions.IgnoreCase
is self-explanatory...
正则表达式匹配修剪,紧接着是0或更多空格(包括制表符)和一个左括号(.RegexOptions.IgnoreCase是不言自明的...
EDIT: add a (?<![$%])
to make it ignore trim
with leading %
or $
like this :
编辑:添加一个(?<![$%])使其忽略修剪前导%或$像这样:
Regex.Replace(text, @"\b(?<![$%])TRIM(?=\s*\()", "RTRIM", RegexOptions.IgnoreCase)
#2
0
You could do a replace like this:
你可以做这样的替换:
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");