3 回答

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊
使用正則表達(dá)式
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);
結(jié)果:
name equal '('John')'

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
使用消極的后視和消極的向前看,如果它遇到,這將停止匹配,例如(?<! )(?! )'
(?<!')\(|\)(?!')
該示例將其解釋為注釋:
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);
結(jié)果
名稱等于“(”約翰“)”

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
試試這個(gè):
var replaced = Regex.Replace("(name equal '('John')')", @"\((.+?'\)')\)", "${1}");
該類位于命名空間中。Regex
System.Text.RegularExpressions
- 3 回答
- 0 關(guān)注
- 177 瀏覽
添加回答
舉報(bào)