简体   繁体   English

正则表达式匹配不起作用

[英]RegEx Match is not working

If the result is not a success the Compiler ist just stopping and debug is also just stopping but there is no error, it's just stopping. 如果结果不成功,则编译器将仅停止,调试也将停止,但是没有错误,那就是停止。 If the Result is a success it works but the else part is not working but what can i do if the compiler is just stopping if the Result is a not success? 如果结果成功,则工作,但其他部分不工作,但是如果结果不成功,编译器只是停止,该怎么办?

Match Result = Regex.Match(file, pattern);

if(Result.Success)
{
    // This part works
}
else
{
    // this is not working
}

There is a try catch arround this, just to mention it. 仅此而已。

else block is only executed if the if block fails. else块仅在if块失败时执行。 In your case the regex is successfully matching so your else{ } isn't executed. 在您的情况下,正则表达式已成功匹配,因此不会执行else{ }

Don't worry if it doesn't match. 如果不匹配,请不要担心。 If regex doesn't match, it will return false and your else{ } will be executed. 如果正则表达式不匹配,它将返回false并执行您的else{ }

I don't think the problem lies in the fact that Success is true or false. 我认为问题不在于成功是对还是错。 I tried this code and it works: 我尝试了这段代码,它的工作原理是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1, s2, s0;
            //Regex regex;
            Match match;
            s0 = "l'insostenibile leggerezza dell'essere";
            s1 = "g.+z";
            s2 = "'.*'";
            try
            {
                match = Regex.Match(s0, s1);
                if(match.Success)
                {
                }
                else
                {
                }
                match = Regex.Match(s0, s2);
                if(match.Success)
                {
                }
                else
                {
                }
                s1 = "x.+r";
                match = Regex.Match(s0, s1);
                if(match.Success)
                {
                }
                else
                {
                }
            }
            catch(Exception ex)
            {
            }

        }
    }
}

Have you checked the clause? 您是否检查过该条款? Replace your clause in my code and try again. 将您的子句替换为我的代码,然后重试。

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

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