如何解决 "Unable to cast object of type 'System.Text.RegularExpressions.Match' to type 'System.IConvertible".error

how to solve "Unable to cast object of type 'System.Text.RegularExpressions.Match' to type 'System.IConvertible".error

当我尝试使用正则表达式匹配将字符串表达式一分为二来使第一个 int 值等于 short 类型的对象时出现错误。

我的代码

 var splitedResolution = sub.ResolutionValue.Split('*');
 var resolutionwidht = Regex.Match(splitedResolution[0], @"\b\d+\.?\d?\b");   
 resolutionReportsub.Width =Convert.ToInt16(resolutionwidht);
                       

Match-方法returns一个Match-对象。该对象还有一个 Success-属性 来确定字符串是否与您的模式匹配。

为了得到实际匹配的字符串使用theMatchObject.Value:

var splitedResolution = sub.ResolutionValue.Split('*');
var resolutionwidht = Regex.Match(splitedResolution[0], @"\b\d+\.?\d?\b");   
resolutionReportsub.Width =Convert.ToInt16(resolutionwidht.Value);