Regex issue in C# to match named capturing groups

Hi!

I'm trying to match the string "http://10.10.10.1:8080" using named capturing groups in C#. The Regex itself works fine when testing. However in C# I get the empty string value after matching against the Regex. Please, see my code. There're two methods I've used. However, non of them sets the value to string variable. What do I do wrong here?

Thanks in advance!
// Method 1
 serverAddress = Regex.Match(proxyAddress, @"(?<IpAddress>\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)|(?<PortNumber>(?:)\d*)", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups["IpAddress"].Value;
                MessageBox.Show(serverAddress);
                port = Regex.Match(proxyAddress, @"(?<IpAddress>\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)|(?<PortNumber>(?:)\d*)").Groups["PortNumber"].Value;
                MessageBox.Show(port);

// Method 2
string patern = @"(?<IpAddress>\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)|(?<PortNumber>(?:)\d*)";
                if (Regex.IsMatch(proxyAddress, patern)){
                    Regex r = new Regex(patern, RegexOptions.IgnoreCase);
                    serverAddress = r.Match(proxyAddress).Result("${IpAddress}");
                    port = r.Match(proxyAddress).Result("${PortNumber}");
                    MessageBox.Show(serverAddress);
                    MessageBox.Show(port);
                }

Open in new window

LVL 1
ZaurbAsked:
Who is Participating?
 
Pui_YunConnect With a Mentor Commented:
Hi Zaurb,
The problem is with your alternation, you're current regex does the following:

captures the ipaddress
or if it isn't then we have a port number which can be zero or more digits


Because of this you can have a bunch of successful matches that have a null value because a port number can have zero digits (i.e. null value)

Now, I'm not sure whether you want to capture the ip address if the port number isn't present, but I will assume so.  So try my attached regex and code (which is modified from yours - I've only changed the regex), I've tested it with expresso and it looks okay to me.

Hope this helps.

P.
// Method 1
 serverAddress = Regex.Match(proxyAddress, @"(?<IpAddress>\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(?:\:(?<PortNumber>\d+))?", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups["IpAddress"].Value;
                MessageBox.Show(serverAddress);
                port = Regex.Match(proxyAddress, @"(?<IpAddress>\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(?:\:(?<PortNumber>\d+))?").Groups["PortNumber"].Value;
                MessageBox.Show(port);

// Method 2
string patern = @"(?<IpAddress>\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(?:\:(?<PortNumber>\d+))?";
                if (Regex.IsMatch(proxyAddress, patern)){
                    Regex r = new Regex(patern, RegexOptions.IgnoreCase);
                    serverAddress = r.Match(proxyAddress).Result("${IpAddress}");
                    port = r.Match(proxyAddress).Result("${PortNumber}");
                    MessageBox.Show(serverAddress);
                    MessageBox.Show(port);
                }

Open in new window

0
 
rg20Commented:
Stolen from
http://dotnetperls.com/regex-match-use

 // First we see the input string.
        string input = "/content/alternate-1.aspx";

        // Here we call Regex.Match.
        Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$",
            RegexOptions.IgnoreCase);

        // Here we check the Match instance.
        if (match.Success)
        {
            // Finally, we get the Group value and display it.
            string key = match.Groups[1].Value;
            Console.WriteLine(key);
        }
0
 
lazyberezovskyCommented:
You can use Match.Success to verify regex matched and match.Groups["IpAddress"] for accessing named groups.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ZaurbAuthor Commented:
Modified the code but still no success.
MessageBoxes still appear empty.
Debuging shows match success and enters in 'if'... but string variables are not assigned any value.
                string serverAddress;
                string port;
                string proxy = "http://10.10.10.1:8080/";

                string pattern = @"(?<IpAddress>\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)|(?<PortNumber>(?:)\d*)";
                Match match = Regex.Match(proxy, pattern);
                if (match.Success)
                {
                    serverAddress = match.Groups["IpAddress"].Value;
                    port = match.Groups["PortNumber"].Value;
                    MessageBox.Show(serverAddress);
                    MessageBox.Show(port);
                }

Open in new window

0
 
rg20Commented:
Try addressing it as 0 and 1
as the collection elements might not have names


if (match.Success)
                {
                    serverAddress = match.Groups[0].Value;
                    port = match.Groups[1].Value;
                    MessageBox.Show(0);
                    MessageBox.Show(1);
                }
0
 
ZaurbAuthor Commented:
Tried. Didn't work.
0
 
rg20Commented:
sorry
MessageBox.Show(serverAddress);
MessageBox.Show(port);
0
 
rg20Commented:
Why not just use a split on the :, then grab the array[1] and array[0]

0 will be the http://
1 = ip
2 = port
0
 
ZaurbAuthor Commented:
Hi Pui Yun,

This works great! You're absolutely right. The question mark in the end of regex is for "greediness" to continue search even after the first found match, is that right?
Thank you very much!
0
 
ZaurbAuthor Commented:
Excellent! Thank you!
0
 
Pui_YunCommented:
Hi Zaurb,
The question mark means match for zero or one, I did this just in case you find a ip address that doesn't contain a port number, so the regex will match for just the ip address and if there is a ":" after the ip address, it will capture the port number as well.

Hope this helps.
P.
0
All Courses

From novice to tech pro — start learning today.