简体   繁体   English

2使用.net中的正则表达式提取值

[英]2extract value using regular expression in .net

"reportstarttime=10-18-2004:10-50-56 reportid= 12345456 reportendtime=10-18-2004:10-50-56" “ reportstarttime = 10-18-2004:10-50-56 reportid = 12345456 reportendtime = 10-18-2004:10-50-56”

How can i extract value of reportid which is 123456 我如何提取reportid的123456

string GetReportId(string data)
{
    var regex = new Regex(" reportid=([0-9]+) ");
    var result = regex.Match(data);
    if (!result.Success) throw new FormatException();
    return result.Groups[1].Value;
}

var result = GetReportId("reportstarttime=10-18-2004:10-50-56 reportid=12345456 reportendtime=10-18-2004:10-50-56");

Is there a pressing need for a regular expression? 迫切需要一个正则表达式吗? What's wrong with good ole Split? 好的ole split有什么问题?

var input = "reportstarttime=10-18-2004:10-50-56 reportid=12345456 reportendtime=10-18-2004:10-50-56";
var value = input.Split(' ')[1].Split('=')[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM