gpt4 book ai didi

c# - 在字符串中搜索特定格式的值

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:07 25 4
gpt4 key购买 nike

如何从这样的字符串中提取值:Lambertseter_InfoCloseDay_07_09_2014_19_55_52?

格式是这样的:<Location>_<TypeOfFile>_<DD>_<MM>_<YYYY>_<HH>_<MM>_<SS>

我试过这个:

        string Mystring = "Lambertseter_InfoCloseDay_07_09_2014_19_55_52";
string[] splittedFileString = Mystring.Split('_');
string[] formatArray = { "Bingohall: ", "Filetype: ", "Day: ", "Month: ", "Year: ", "Hour: ", "Minute: ", "Second: " };

for (int i = 0; i < formatArray.Length; i++)
{
formatArray[i] += splittedFileString[i];
}

这似乎至少有效,但我这样做是否正确?有没有更简单的方法以这种方式管理字符串?

最佳答案

如果您的数据是这样的正则,则此正则表达式可以工作:

var regex = new Regex(@"(?<Location>\w+)_(?<TypeOfFile>\w+)_(?<DD>\d+)_(?<MM>\d+)_(?<YYYY>\d+)_(?<HH>\d+)_(?<MIN>\d+)_(?<SS>\d+)", RegexOptions.ExplicitCapture);
var match = regex.Match("Lambertseter_InfoCloseDay_07_09_2014_19_55_52");
var groups = match.Groups;
var output = $"Bingohall: {groups["Location"].Value}, Filetype: {groups["TypeOfFile"].Value}, Day: {groups["DD"].Value}, Month: {groups["MM"].Value}, Year: {groups["YYYY"].Value}, Hour: {groups["HH"].Value}, Minute: {groups["MIN"].Value}, Second: {groups["SS"].Value}";

关于c# - 在字符串中搜索特定格式的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25749833/

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