3 回答
TA贡献1820条经验 获得超10个赞
使用正则表达式
string input = "(name equal '('John')')";
Regex rx = new Regex(@"^\((.*?)\)$");
Console.WriteLine(rx.Match(input).Groups[1].Value);
使用子字符串方法
String input= "(name equal '('John')')";
var result = input.Substring (1, input.Length-2);
Console.WriteLine(result);
结果:
name equal '('John')'
TA贡献1829条经验 获得超7个赞
使用消极的后视和消极的向前看,如果它遇到,这将停止匹配,例如(?<! )(?! )'
(?<!')\(|\)(?!')
该示例将其解释为注释:
string pattern =
@"
(?<!')\( # Match an open paren that does not have a tick behind it
| # or
\)(?!') # Match a closed paren tha does not have tick after it
";
var text = "(name equal '('John')')";
// Ignore Pattern whitespace allows us to comment the pattern ONLY, does not affect processing.
var final = Regex.Replace(text, pattern, string.Empty, RegexOptions.IgnorePatternWhitespace);
结果
名称等于“(”约翰“)”
TA贡献1797条经验 获得超4个赞
试试这个:
var replaced = Regex.Replace("(name equal '('John')')", @"\((.+?'\)')\)", "${1}");
该类位于命名空间中。Regex
System.Text.RegularExpressions
- 3 回答
- 0 关注
- 127 浏览
添加回答
举报