2 回答

TA貢獻1853條經驗 獲得超9個贊
您可以嘗試Regex.Replace:我們將每個A..Za..z
匹配項替換為自身,$0
后跟一個新行
string source = "184.b189.a194.b199.d204.d209.b214.b219.d";
string result = Regex.Replace(source, "[A-Za-z]", $"$0{Environment.NewLine}");
Console.Write(result);
結果:
184.b
189.a
194.b
199.d
204.d
209.b
214.b
219.d
如果你想添加相同的想法<br />
string result = Regex.Replace(source, "[A-Za-z]", $"$0<br />");
Linq是另一種選擇:
string result = string.Concat(source
.Select(c => c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'
? c.ToString() + "<br />"
: c.ToString()));

TA貢獻1993條經驗 獲得超6個贊
您可以使用正則表達式(\d{3}\.[A-Za-z])
https://regex101.com/r/Z05cC4/1,
這是:
\d{3} matches a digit (equal to [0-9])
{3} Quantifier — Matches exactly 3 times
\. matches the character . literally (case sensitive)
Match a single character present in the list below [A-Za-z]
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
然后只拿第一組。
public static class Program
{
private static void Main(string[] args)
{
string input = @"184.b189.a194.b199.d204.d209.b214.b219.d";
IEnumerable<string> capturedGroups = ExtractNumbers(input);
string res = string.Join(Environment.NewLine, capturedGroups);
Console.WriteLine(res);
}
static IEnumerable<string> ExtractNumbers(string Input)
{
string pattern = @"(\d{3}\.[A-Za-z])";
MatchCollection matches = Regex.Matches(Input, pattern, RegexOptions.Singleline);
foreach (Match match in matches)
yield return match.Groups[1].Value;
}
}
輸出:
184.b
189.a
194.b
199.d
204.d
209.b
214.b
219.d
- 2 回答
- 0 關注
- 140 瀏覽
添加回答
舉報