正则表达式匹配字符串中给定的字符串文件名

时间:2012-06-29 10:13:33

标签: c# asp.net regex

我有一个字符串

  

PrintFileURL(“13572_BranchInformationReport_2012-06-29.zip”,“13572_BranchInformationReport_2012-06-29.zip”,0,“184277”,“Jun 29 1:30”,“/ icons / default.gif”)

另外

  

PrintFileURL( “13572_IndividualInformationReportDelta_2012-06-29_033352.zip”, “13572_IndividualInformationReportDelta_2012-06-29_033352.zip”,0, “53147”,“君   29 3:33“,”/ icons / default.gif“)

什么是正则表达式如果我想使用c#...从上面的字符串中提取13572_IndividualInformationReportDelta_2012-06-29_033352.zip13572_BranchInformationReport_2012-06-29.zip

2 个答案:

答案 0 :(得分:1)

这个文件应该完全适用于前两个文件,但如果文件名中有任何其他特殊字符则不起作用:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const string example = "PrintFileURL(\"13572_IndividualInformationReportDelta_2012-06-29_033352.zip\",\"13572_IndividualInformationReportDelta_2012-06-29_033352.zip\",0,\"53147\",\"Jun 29 3:33\",\"/icons/default.gif\")";
            Console.WriteLine(example);

            const string pattern = "\\\"([a-zA-Z0-9\\-_]*?\\..*?)\\\"";
            var regex = new Regex(pattern);
            var result = regex.Matches(example);
            foreach (Match item in result)
            {
                Console.WriteLine(item.Groups[1]);
            }
        }
    }
}

答案 1 :(得分:0)

你可以尝试这个:

Regex

然后

var regex = new Regex("@(?<=\")(.*?)(?=\")");

var result = regex.Matches(example);
foreach (Match item in result)
{
    Console.WriteLine(item.Groups[1]);
}