开发者

How to access variables inside regex statements? c# 4.0

开发者 https://www.devze.com 2023-04-13 06:25 出处:网络
I have looked everywhere, but I cannot for the life of me figure out how to make a variable inside a regex statement be accessible from elsewhere.

I have looked everywhere, but I cannot for the life of me figure out how to make a variable inside a regex statement be accessible from elsewhere.

If someone could help that would be amazing! Here is the code:

string strRegex = @"(regexstring)";

        Reg开发者_如何学编程exOptions myRegexOptions = RegexOptions.Multiline;
        Regex myRegex = new Regex(strRegex, myRegexOptions);
        string strTargetString = str9

        foreach (Match myMatch in myRegex.Matches(strTargetString))
        {
            if (myMatch.Success)
            {

Here is the problem --> . . .................. string str5 = myMatch.ToString();

            }

webBrowser1.navigate(str5); <-- This doesnt work

How Do I access the string str5? outside of the loop? Please Help


If you want to access the variable outside the loop, it needs to be declared outside the loop. However, you then need to consider:

  • What do you want to happen if there weren't any matches?
  • What do you want to happen if there were multiple matches?

You'll need to assign a variable to it outside the loop as otherwise when you try to read the variable, the compiler will complain because it's not definitely assigned.


Perhaps you want:

string str5 = myMatch.Value;
webBrowser1.navigate(str5);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号