2 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用
(?<special>[+^%]*(?:\([^)]*\)|{[^}]*}))|(?<text>[\w\s]+)
請參閱正則表達(dá)式演示。
細(xì)節(jié)
(?<special>[+^%]*(?:\([^)]*\)|{[^}]*}))
- 組“特殊”捕獲:\([^)]*\)
- a(
,然后是 0+ 字符)
,然后是 a)
|
- 或者{[^}]*}
- a{
,然后是 0+ 字符}
,然后是 a}
[+^%]*
- 零個(gè)或多個(gè)+
,^
或%
字符(?:
-匹配以下兩種選擇之一的非捕獲組:)
- 非捕獲組的結(jié)束。|
- 或者(?<text>[\w\s]+)
- 組“文本”:一個(gè)或多個(gè)單詞或空格字符。

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊
嘗試以下:
string input = "Text 1^%+{TAB}({CMD 1}{CMD 2})Text 2.^(abc)";
string pattern = @"^(?'text1'[^\^]+)(?'special1'[^\(]+)(?'special2'[^\)]+\))(?'text2'[^\^]+)(?'special3'.*)";
Match match = Regex.Match(input, pattern);
Console.WriteLine("Text : '{0}' Special : '{1}' Special : '{2}' Text : '{3}' Special : '{4}'",
match.Groups["text1"].Value,
match.Groups["special1"].Value,
match.Groups["special2"].Value,
match.Groups["text2"].Value,
match.Groups["special3"].Value
);
Console.ReadLine();
- 2 回答
- 0 關(guān)注
- 437 瀏覽
添加回答
舉報(bào)