用于从字符串中提取特定编号模式的C#Regex

用于从字符串中提取特定编号模式的C#Regex,c#,regex,string,search,C#,Regex,String,Search,我成功地提取了一些特定模式的数字,如:“F2020-53、2020-54、2020-56 John Doe”,在使用时,我得到了“2020-53”、“2020-54”和“2020-56” Regex fakturaMultiPattern = new Regex(@"(\d{4}-\d+)+"); 但我无法提取这样的模式:“F2020-53/54 John Doe”,我想提取“2020-53/54”。我试过: Regex MultiSlashPattern = new Re

我成功地提取了一些特定模式的数字,如:“F2020-53、2020-54、2020-56 John Doe”,在使用时,我得到了“2020-53”、“2020-54”和“2020-56”

Regex fakturaMultiPattern = new Regex(@"(\d{4}-\d+)+");
但我无法提取这样的模式:“F2020-53/54 John Doe”,我想提取“2020-53/54”。我试过:

Regex MultiSlashPattern = new Regex(@"^\d{4}-\d+/\d+");

我想,我快到了,但请帮帮我

这应该满足您的期望:

using System;
using System.Text.RegularExpressions;

namespace Reginator
{
    class Program
    {
        static void Main(string[] args)
        {
            var testString = @"F2020-53/54, 2020-54, 2020-56 John Doe";
            var matches = GetMatchedCollections(testString, @"([,]?[0-9]+\-[0-9]+\/[0-9]+|[,]?[0-9]+-[0-9]+)");

            Console.WriteLine(matches[0].ToString());
            Console.WriteLine(matches[1].ToString());
            Console.WriteLine(matches[2].ToString());

            // Output:
            //      2020-53/54
            //      2020 - 54
            //      2020 - 56
        }

        public static MatchCollection GetMatchedCollections(string stringToSearch, string pattern)
        {
            var regex = new Regex(pattern, RegexOptions.IgnoreCase);
            return regex.Matches(stringToSearch);
        }
    }
}

您是否尝试过从正则表达式的开头删除
^
?是的,我尝试过。我应该告诉你我使用了Split()方法,所以^anchor应该在那里。这个问题一定与“/\d+”有关,这是我最确定的-谢谢你,我的朋友。我真的需要坐下来,花几个小时学习更多关于日常表达的知识,不用担心。如果对它满意,请不要忘了标记为解决方案。我觉得很愚蠢,我在哪里标记为解决方案?不用担心。上述代码左侧有一个勾号。你只看到它。:)