C# 有没有可能简化这个try-catch和if逻辑?
我有以下代码。它按预期工作,但我只是想知道我是否可以改变它背后的逻辑,使它看起来更简单,因为我相信它是多余的 它的工作原理是这样的:我获取一些数据,然后尝试通过正则表达式传递它。有三种可能的结果:C# 有没有可能简化这个try-catch和if逻辑?,c#,.net,try-catch,C#,.net,Try Catch,我有以下代码。它按预期工作,但我只是想知道我是否可以改变它背后的逻辑,使它看起来更简单,因为我相信它是多余的 它的工作原理是这样的:我获取一些数据,然后尝试通过正则表达式传递它。有三种可能的结果: 数据提取正常 数据已提取,但不包含数字。因此,通过了另一个正则表达式 数据未被提取通过了与2)相同的正则表达式。(我认为这是可以简化或优化的) 起初,我认为有一种方法可以在不引发异常的情况下检查正则表达式是否返回空,但是(如果我错了,请纠正我)没有这样的事情。。。所以这就是为什么我包括了try/cat
m2[0].Groups[1].Captures[0].Value
超出范围(因为它是空的)
所以。。。有没有办法让它看起来更优化?
多谢各位
string regexString = @" \.\.\. .*?>(.*?)<\/a>";
MatchCollection m2 = Regex.Matches(myInput, regexString, RegexOptions.Singleline);
try
{
if (m2[0].Groups[1].Captures[0].Value.All(Char.IsDigit) == false)
{
regexString = @"page=.*?"">(.*?)</a>\n.*?<a class=""pagebtn""";
m2 = Regex.Matches(myInput, regexString);
}
}
catch (ArgumentException)
{
regexString = @"page=.*?"">(.*?)</a>\n.*?<a class=""pagebtn""";
m2 = Regex.Matches(myInput, regexString);
}
string regexString=@“\.\.\.\..*?>(.*?”;
MatchCollection m2=Regex.Matches(myInput、regexString、RegexOptions.Singleline);
尝试
{
if(m2[0].Groups[1].捕获[0].Value.All(Char.IsDigit)==false)
{
regexString=@“page=.*?”>(.*)\n.*(.*)\n.*?您可以使用IsMatch检查它,如果不匹配,请尝试第二个regex,如果不匹配,则不匹配
Regex normalRegex = new Regex(@" \.\.\. .*?>(.*?)<\/a>", RegexOptions.SingleLine);
Regex secondaryRegex = new Regex(@"page=.*?"">(.*?)</a>\n.*?<a class=""pagebtn""");
int valueFound;
bool numberParsed;
if (normalRegex.IsMatch(myInput)) {
// your code to check here
// use int.TryParse to parse your value and add the result to
numberParsed = int.TryParse(m2[0].Groups[1].Captures[0].Value, out valueFound);
}
if (!numberParsed) {
if (secondaryRegex.IsMatch(myInput)) {
// second code matches
} else {
// no match
}
}
Regex normalRegex=newregex(@“\.\.\.\..*?>(.*),RegexOptions.SingleLine);
Regex secondaryRegex=new Regex(@“page=.*?”>(.*?)\n.*?您可以使用IsMatch检查它,如果不匹配,请尝试第二个Regex,如果不匹配,则不匹配
Regex normalRegex = new Regex(@" \.\.\. .*?>(.*?)<\/a>", RegexOptions.SingleLine);
Regex secondaryRegex = new Regex(@"page=.*?"">(.*?)</a>\n.*?<a class=""pagebtn""");
int valueFound;
bool numberParsed;
if (normalRegex.IsMatch(myInput)) {
// your code to check here
// use int.TryParse to parse your value and add the result to
numberParsed = int.TryParse(m2[0].Groups[1].Captures[0].Value, out valueFound);
}
if (!numberParsed) {
if (secondaryRegex.IsMatch(myInput)) {
// second code matches
} else {
// no match
}
}
Regex normalRegex=newregex(@“\.\.\.\..*?>(.*),RegexOptions.SingleLine);
Regex secondaryRegex=new Regex(@“page=.*?”>(.*)\n.*?我假设对您的问题更准确的描述是,您希望解析一个值,该值可能是整数,也可能不是整数,但您的m2[0]。组[1]。捕获[0]。值。如果value
为null
,则抛出所有(Char.IsDigit)
然后可以简化为以下内容:
int parsedValue = 0;
if (m2[0].Success)
{
var value = m2[0].Groups[1].Captures[0].Value;
if (!string.IsNullOrWhiteSpace(value))
{
if (int.TryParse(value, out parsedValue))
{
// do something with parsedValue, or with the fact that the value exists and is an integer
}
}
}
我假设对您的问题更准确的描述是,您希望解析一个值,该值可能是整数,也可能不是整数,但您的m2[0]。组[1]。捕获[0]。值。如果value
为null
,则所有(Char.IsDigit)
都会抛出
然后可以简化为以下内容:
int parsedValue = 0;
if (m2[0].Success)
{
var value = m2[0].Groups[1].Captures[0].Value;
if (!string.IsNullOrWhiteSpace(value))
{
if (int.TryParse(value, out parsedValue))
{
// do something with parsedValue, or with the fact that the value exists and is an integer
}
}
}
您应该解决该异常。哪个特定语句抛出,m2[0]。组[1]。捕获[0]。我想是值吧?这是字符串,所以是string.IsNullOrWhiteSpace()
就足够了。Regex
实例有一个可以检查的bool IsMatch
属性。解决它的唯一方法是检查Regex结果是否为空。这是我问题的一部分,因为我不知道是否可以这样做。这就是我使用try/catch的原因。我错了,你应该检查,所以m2[0].Success
。在可能的情况下,最好避免在控制流中使用异常。当我审核代码库时,我会寻找以前的程序员吞没异常的地方,这样的做法会使工作更加困难。您可以使用扩展方法将其转换为var allDigits=m2.safety(0,m=>m.Groups.safety(1,g=>g.Captures.safety(0,c=>c.Values.All(Char.IsDigit))
您应该解决异常。哪个特定语句抛出,m2[0]。组[1]。捕获[0]。我想是值吧?那是字符串,所以是string.IsNullOrWhiteSpace()
就足够了。Regex
实例有一个可以检查的bool IsMatch
属性。解决它的唯一方法是检查Regex结果是否为空。这是我问题的一部分,因为我不知道是否可以这样做。这就是我使用try/catch的原因。我错了,你应该检查,所以m2[0].Success
。在可能的情况下,最好避免在控制流中使用异常。当我审核代码库时,我会寻找以前的程序员吞没异常的地方,这样的做法会使工作更加困难。您可以使用扩展方法将其转换为var allDigits=m2.safety(0,m=>m.Groups.safety(1,g=>g.Captures.safety(0,c=>c.Values.All(Char.IsDigit)))
有了IsMatch,我需要确切地知道结果是什么。我不应该使用。另一个答案建议的成功吗?也许是这样,但你可以很容易地调整它的逻辑,我编辑了我的帖子@Arturo。有了IsMatch,我需要确切地知道结果是什么。我不应该使用。另一个答案建议的成功吗?也许是这样,但你可以很容易地做到根据它的逻辑,我编辑了我的博文@Arturo