0

我在 C# 中有任何文本,我需要使用正则表达式“匹配”,并获取一个值(解析文本以获取值)。

文本:

var asunto1 = "ID P20101125_0003 -- Pendiente de autorización --";

var asunto2 = "ID P20101125_0003 任何文本任何文本";

var asunto3 = "ID_P20101125_0003 任何文本任何文本";

我需要获得价值:

var peticion = "P20101125_0003";

我有这个正则表达式,但对我来说失败了:

    //ID P20101125_0003 -- Pendiente de autorización --

            patternPeticionEV.Append(@"^");
            patternPeticionEV.Append(@"ID P");
            patternPeticionEV.Append(@"(20[0-9][0-9])"); // yyyy
            patternPeticionEV.Append(@"(0[1-9]|1[012])"); // MM
            patternPeticionEV.Append(@"(0[1-9]|[12][0-9]|3[01])"); // dd
            patternPeticionEV.Append(@"(_)"); 
            patternPeticionEV.Append(@"\d{4}");
            //patternPeticionEV.Append(@"*");
            patternPeticionEV.Append(@"$");

if (System.Text.RegularExpressions.Regex.IsMatch(asuntoPeticionEV, exprRegular, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                var match = System.Text.RegularExpressions.Regex.Match(asuntoPeticionEV, exprRegular, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//...
            }
4

4 回答 4

3

Your regular expression ends with "$" which says "the line/text has to end there". You don't want that. Just get rid of this line:

patternPeticionEV.Append(@"$");

and it will mostly work immediately. You then just need to add a capturing group to isolate the bit of text that you want.

I'd also recommend adding using System.Text.RegularExpressions; so that you don't have to fully qualify Regex each time. You can also call Match and then check for success, to avoid matching it twice.

Sample code:

using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        DisplayMatch("ID P20101125_0003 -- Pendiente de autorización --");
        // No match due to _
        DisplayMatch("ID_P20101125_0003 any text any text");
    }

    static readonly Regex Pattern = new Regex
        ("^" + // Start of string
         "ID " +
         "(" + // Start of capturing group
         "P" +
         "(20[0-9][0-9])" + // yyyy
         "(0[1-9]|1[012])" + // MM
         "(0[1-9]|[12][0-9]|3[01])" + // dd
         @"_\d{4}" +
         ")" // End of capturing group
         );

    static void DisplayMatch(string input)
    {
        Match match = Pattern.Match(input);
        if (match.Success)
        {
            Console.WriteLine("Matched: {0}", match.Groups[1]);
        }
        else
        {
            Console.WriteLine("No match");
        }
    }
}
于 2010-11-26T10:16:02.993 回答
1

这可能只是我,但对于将字符串解析为有意义的值之类的事情,我更喜欢做一些更冗长的事情,比如:

    private bool TryParseContent(string text, out DateTime date, out int index)
    {
        date = DateTime.MinValue;
        index = -1;

        if (text.Length < 17)
            return false;

        string idPart = text.Substring(0, 4);

        if (idPart != "ID_P" && idPart != "ID P")
            return false;

        string datePart = text.Substring(4, 8);

        if (!DateTime.TryParseExact(datePart, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None, out date))
            return false;

        // TODO: do additional validation of the date

        string indexPart = text.Substring(13, 4);

        if (!int.TryParse(indexPart, out index))
            return false;

        return true;
    }
于 2010-11-26T10:44:53.597 回答
0

为什么不使用如下子字符串:

var asunto1 = "ID P20101125_0003 -- Pendiente de autorización --";
var asunto2 = "ID P20101125_0003 any text any text";
var asunto3 = "ID_P20101125_0003 any text any text";

var peticion = asunto1.Substring(3,14); //gets P20101125_0003
于 2010-11-26T10:17:50.200 回答
0

这个正则表达式会给你想要的字符串

^ID[_ ]P[0-9_]+?
于 2010-11-26T10:24:17.440 回答