0

我是正则表达式的新手。我想解析以下数据。我想出了一些似乎适用于 sublime 的正则表达式,但是当我在 Visual Studio 中对其进行测试时,它就不起作用了。我想知道是否有人可以提供一个简单的例子或指导如何使用正则表达式解析它。它一定很简单,一定是我理解的错误,所以提前道歉。

这是我正在处理的数据。

Fri 11:00 - 12:00
                            Max Agents: 1
                            Min Agents: 2
                            Total Calls: 3
                            Answered Calls: 4
                            Abandoned Calls: 5
                            Average Time to Answer (secs): 6
                            Longest Time to Answer (secs): 7
                            Average Time in Call (secs): 8
                            Longest Time in Call (secs): 9
                            Average Time before Abandon (secs): 10
                            Per agent statistics:
                              Agent: 1001
                                From Direct Call:
                                  Total Calls Answered : 11
                                  Average Time in Call (secs) : 12
                                  Longest Time in Call (secs) : 13
                                From Queue:
                                  Total Calls Answered : 2
                                  Average Time in Call (secs) : 14
                                  Longest Time in Call (secs) : 15
                              Agent: 1002
                                From Direct Call:
                                  Total Calls Answered : 1
                                  Average Time in Call (secs) : 16
                                  Longest Time in Call (secs) : 17
                                From Queue:
                                  Total Calls Answered : 2
                                  Average Time in Call (secs) : 18
                                  Longest Time in Call (secs) : 19
                            Queue related statistics:
                              Total calls presented to the queue: 20
                              Calls answered by agents: 21
                              Number of calls in the queue: 22
                              Average time to answer (secs): 23
                              Longest time to answer (secs): 24
                              Number of abandoned calls: 25
                              Average time before abandon (secs): 26
                              Calls forwarded to voice mail: 27
                              Calls answered by voice mail: 28
                              Number of error calls: 29

这是唯一一个获得 Agent:1004 的部分。

Agent:.(?<agentNum>\d+)\n?((?:[a-z\s]+from.*\n)+\s(?:[a-z\s]+call.*\n)+)?((?:[a-z\s]+from.*\n)+[\n\s]+(?:[a-z\s]+call.*\n)+)?

我正在尝试提取不同属性的数据,例如总呼叫回答平均通话时间

等等 。我基本上想为这些字段提取数据并存储在一个表中。

4

3 回答 3

2

只要文本格式正确并且总是以相同的顺序打印出来,我根本不会使用正则表达式,我会逐行解析文本并使用函数解析出来。

class CallCenterActivity
{
    public CallCenterActivity(string callActivity)
    {
        AgentStistics = new List<AgentStatistic>();

        using(var reader = new StringReader(callActivity))
        {
             ActivityDate = ParseActivityDate(reader.ReadLine());
             MaxAgents = ExtractInt(reader);
             MinAgents = ExtractInt(reader);
             //(Snip)
             AvarageTimeBeforeAbandon = ExtractInt(reader);

             if(reader.ReadLine().Trim().Equals("Per agent statistics:") == false)
                 throw new InvalidDataException("We where not on the line we expected to be for \"Per Agent statistics:\"");

             string currentLine;
             //This loops till we break out of the agent section
             while((currentLine = reader.ReadLine()).Trim().Equals("Queue related statistics:") == false)
             {
                  var agent = new AgentStatistic();
                  agent.AgentId = ExtractInt(reader);
                  agent.DirectCallsAnswered = ExtractInt(reader);
                  //(snip)
                  agent.QueueLongestTimeInCall = ExtractInt(reader);

                  AgentStistics.Add(agent);
             }

             TotalCallsPresentedToQueue = ExtractInt(reader);
             //(Snip)
             CallsAnsweredByVoiceMail = ExtractInt(reader);
        }

    }

    //These parser methods are small and kept static so you could easily write unit tests against each parser.
    private static DateTime ParseActivityDate(string activityDateLine)
    {
        throw new NotImplmentedException("Here you would turn your \"Fri 11:00 - 12:00\" in to a DateTime");
    }

    //ParseInt and ExtractInt are separated to ease Unit Testing.
    private static int ParseInt(string line)
    {
        var split = line.Split(':')
        return Int32.Parse(split[1]);
    }

    private static int ExtractInt(StringReader reader)
    {
        return ParseInt(reader.ReadLine());
    }

    public DateTime ActivityDate {get;set;}
    public int MaxAgents {get;set;}
    public int MinAgents {get;set;}
    public int TotalCalls {get;set;}
    public int AnsweredCalls {get;set;}
    public int AbandonedCalls {get;set;}
    public int AvarageTimeToAnswer {get;set;}
    public int LongestTimeToAnswer {get;set;}
    public int AvarageTimeBeforeAbandon {get;set;}
    public List<AgentStatistic> AgentStistics {get; private set;}
    public int TotalCallsPresentedToQueue {get;set;}
    public int CallsAnsweredByAgents {get;set;}
    public int NumberOfCallsInTheQueue {get;set;}
    public int AvarageTimeToAnswerQueue {get;set;}
    public int LongestTimeToAnswerQueue {get;set;}
    public int NumberOfAbandondCalls {get;set;}
    public int AvarageTimeBeforeAbandon {get;set;}
    public int CallsForwaredToVoiceMail {get;set;}
    public int CallsAnsweredByVoiceMail {get;set;}
}

class AgentStatistic
{
    public int AgentId {get;set;}
    public int DirectCallsAnswered {get;set;}
    public int DirectCallsAverageTimeInCall {get;set;}
    public int DirectCallsLongestTimeInCall {get;set;}
    public int QueueAnswered {get;set;}
    public int QueueAverageTimeInCall {get;set;}
    public int QueueLongestTimeInCall {get;set;}
}
于 2013-08-30T16:30:33.460 回答
0

.在你的正则表达式中使用了很多,我建议你避免它。相反,通过匹配[anything that's not what I'minterested ]来匹配一大组你不感兴趣的字符。在这种情况下,您想要数字,对吗?

我会这样做(\D用大写 D 表示 [任何不是数字的东西]):

Agent:\s+(?<AgentNum>\d+)(?:\D+)(?<DirectTotal>\d+)(?:\D+)(?<DirectAvg>\d+)(?:\D+)(?<DirectLongest>\d+)(?:\D+)(?<QueueTotal>\d+)(?:\D+)(?<QueueAvg>\d+)(?:\D+)(?<QueueLongest>\d+)

我假设数据集总是像你的例子一样格式化,行序从未改变,数字没有省略(而是设置为 0)。

我没有 C# 或任何东西来测试我是否使用了(?<NameSubgroup>matchPattern)正确的方法,而且我不熟悉 C# 必须遍历匹配项并提取命名子组的方法,但这应该会让你走上正确的道路。它给了我对reFiddle.com的期望。

内部确保非数字组不会被我使用的语言中的子组捕获?:(?:\D+)

于 2013-08-30T16:21:23.630 回答
0

不确定您要做什么,但这里是您如何使用正则表达式解析字符串的一部分。

Regex ex = new Regex("[0-9]*"); //generic regular expression -- look for numbers
Match match = ex.Match(searchString, startAtIndex);
while(match.Success)
{
    string currentMatch = match.Value;

     //do something


    match = match.NextMatch()
 }
于 2013-08-30T15:44:15.593 回答