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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
Pui_YunCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.