gpt4 book ai didi

c# - 从c#中的字符串中拆分数字

转载 作者:太空宇宙 更新时间:2023-11-03 19:11:06 34 4
gpt4 key购买 nike

我在 C# 中有一个这样的字符串:

{ name: "Phai dấu cuộc tình", mp3: "audio\\16\\Phai dau cuoc tinh.mp3"},{ name: "Caravan of life", mp3: "audio\\4\\Caravan of life.mp3"},{ name: "I'm Forbidden", mp3: "audio\\11\\I'm Forbidden.mp3"},{ name: "Cause i love you", mp3: "audio\\6\\Cause i love you.mp3"},{ name: "Chỉ là giấc mơ", mp3: "audio\\8\\Chi la giac mo.mp3"},{ name: "Lột xác ", mp3: "audio\\12\\Lot xac.mp3"

我想将“\\”之间的数字获取到一个新字符串。例如,结果将是:16;4;11;6;8;12。任何帮助都会很棒。

最佳答案

使用积极的环视断言:

string str = "{ name: \"Phai dấu cuộc tình\", mp3: \"audio\\16\\Phai dau cuoc tinh.mp3\"},{ name: \"Caravan of life\", mp3: \"audio\\4\\Caravan of life.mp3\"},{ name: \"I'm Forbidden\", mp3: \"audio\\11\\I'm Forbidden.mp3\"},{ name: \"Cause i love you\", mp3: \"audio\\6\\Cause i love you.mp3\"},{ name: \"Chỉ là giấc mơ\", mp3: \"audio\\8\\Chi la giac mo.mp3\"},{ name: \"Lột xác\", mp3: \"audio\\12\\Lot xac.mp3\"}";

foreach (var match in Regex.Matches(str, @"(?<=\\)\d+(?=\\)"))
Console.WriteLine(match);

备选方案:捕获组

foreach (Match match in Regex.Matches(str, @"\\(\d+)\\"))
Console.WriteLine(match.Groups[1]);

ideone

关于c# - 从c#中的字符串中拆分数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20548415/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com