shortlin
meaty
請你用C#寫出將字串"{xxxx}ddddd{yyyyy}"取出所有{}裡面的字串放入一個字串陣列
也就是說該陣列會是[xxxx,yyyyy]
meaty
shortlin : 可以使用正則表達式來達成這個目標,以下是使用 C# 的範例程式碼:

``csharp
using System;
using System.Text.RegularExpressions;

class MainClass {
public static void Main (string[] args) {
string input = "{xxxx}ddddd{yyyyy}";
string pattern = @"\{([^}]*)\}";
MatchCollection matches = Regex.Matches(input, pattern);
meaty
string[] results = new string[matches.Count];
for (int i = 0; i < matches.Count; i++) {
results[i] = matches[i].Groups[1].Value;
}
Console.WriteLine(string.Join(",", results));
}
}
``

這個程式碼會輸出 "xxxx,yyyyy",就是所有 {} 中的字串組成的陣列。解釋一下正則表達式:
meaty
- \{\} 代表左右花括號。
- [^}] 代表除了右花括號以外的任何字元。
- * 代表前一個字元重覆出現 0 次或多次。
- () 代表把 () 包圍的部分當成一個群組(group),即把左右花括號之間的部分當成一個群組。
- [^}]* 就是只有左右花括號之間的字元,並重覆出現 0 次或多次。
- Groups[1] 表示取第一個群組,也就是只有左右花括號之間的部分,並將其放到字串陣列中。
shortlin
嗯他這一舉例我才想起來我取人聲log就是用正規
正規真的是用得好很方便
但我越來越懶得去理解他了都直接複製貼上了
載入新的回覆