2

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)";

        RegexOptions 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

2 Answers2

2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Perhaps you want:

string str5 = myMatch.Value;
webBrowser1.navigate(str5);
spender
  • 117,338
  • 33
  • 229
  • 351