0

私は正規表現が初めてです。次のデータを解析したいと思います。崇高に機能するように見える正規表現をいくつか思いつきましたが、ビジュアルスタジオでテストすると機能しません。正規表現を使用してこれを解析する方法について、誰かが簡単な例またはガイダンスを提供できるかどうか疑問に思っていました。それは非常に単純でなければならず、私の間違いに違いないので、あらかじめお詫び申し上げます。

これが私が扱っているデータです。

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

何をしようとしているのかわかりませんが、正規表現を使用して文字列の一部を解析する方法を次に示します。

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 に答える