简体   繁体   中英

Regex not catching multiple matches

Let's say I have the following code for a C# console app.

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

namespace stupidconsoleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            Regex regx = new Regex(@"^(?:https?://)?(?:[\w]+\.)(?:\.?[\w]{2,})+$");

            string test = Console.ReadLine();

            foreach (Match match in regx.Matches(test))
                Console.WriteLine("Match: " + match.Value);

            Console.WriteLine("Press any key to exit.");

            Console.ReadKey();
        }
    }
}

I give it "http://www.cnn.com" and it tells me that this phrase matched; I run it again and give it "www.cnn.com" and again I get a match; I run it again and give it "cnn.com" and get my third successful match.

But, if I just type in "http://www.cnn.com www.cnn.com cnn.com" I get no matches.

Why is this? Is my regex wrong?

Your regex starts with ^ and ends with $ - this indicates that it should only match whole strings. The ^ matches the beginning of the line (or string) and $ matches the end - if there is more text at the end of the string after the first valid URL, it will not match.

If you want all matching sections of the string, remove ^ and $ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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