简体   繁体   English

使用C#正则表达式从字符串中获取多个数据

[英]Using C# regex to get multiple data out of a string

Below I have a sample of a line from a log file that I have been working with for a while now: 下面是我已经使用了一段时间的日志文件中的一行示例:

5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found.

In this file I have categories that I need to capture: First is the date and time, the next is the Locator which is the NXRMN5, Savings found and the percent (if any) which in this line is $257.18 11.5%. 在此文件中,我需要捕获以下类别:首先是日期和时间,其次是Locator,它是NXRMN5,发现的节省以及此行中的百分比(如果有),为$ 257.18 11.5%。

In the Console I would want it to look like this: 在控制台中,我希望它看起来像这样:

Date:  5/21/2015 11:55:56 PM 
Locator: NXRMN5
Dollar Savings: $257.18 
Percent Savings: 11.55 %

Below I have some code that I have been working on for quite some time: 下面有一些我已经工作了一段时间的代码:

foreach (string line in gdsLines)
        {
            Match m = Regex.Match(line, @"Savings\s+found:\s*\$(?<savings>\d+\.\d+)\s*\(\s*(?<percent>\d+\.\d+)\s*%");
            if (m.Success)
            {
                decimal gdsNumberSavings = decimal.Parse(m.Groups["savings"].Value, CultureInfo.InvariantCulture);
                decimal gdsNumberPercent = decimal.Parse(m.Groups["percent"].Value, CultureInfo.InvariantCulture);

                Console.WriteLine("Dollar Savings: $"  + gdsNumberSavings + " Percent Savings: " + gdsNumberPercent + " % " + "\n");
                gdsTotalSavings += gdsNumberSavings;
                gdsTotalPercentage += gdsNumberPercent;
            }
        } 

This code that I provided takes the regex I made and then puts both the savings found and the percent and then prints the values in the console. 我提供的这段代码使用我制作的正则表达式,然后将找到的节省量和百分比都放入,然后在控制台中打印值。 Don't worry about the gdsTotal variables that for something else. 不用担心gdsTotal变量还有其他gdsTotal The main concern is in the regex. 主要关注的是正则表达式。 In order to add in the other elements that I need to, do I need to create another regex, or can I just add on to it? 为了添加我需要的其他元素,是否需要创建另一个正则表达式,还是可以在其上添加? Either way, could someone please help me in the forming of this regex? 不管哪种方式,有人可以帮助我形成此正则表达式吗? Any help would be very appreciative! 任何帮助将不胜感激!

Let me suggest this solution: 让我提出以下解决方案:

  1. Date is extracted by mere splitting the string with | 仅用|分割字符串即可提取日期| and getting the first element 并获得第一个元素
  2. The Locator is captured with regex. 定位器使用正则表达式捕获。

The code is as follows: 代码如下:

string line = "5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found.";
string date = line.Split('|').Take(1).FirstOrDefault().Trim();
Match m = Regex.Match(line, @"Processed\s+(?<locator>[^.]+)\.{3}.*?Savings\s+found:\s*\$(?<savings>\d+\.\d+)\s*\(\s*(?<percent>\d+\.\d+)\s*%");
if (m.Success)
{
     decimal gdsNumberSavings = decimal.Parse(m.Groups["savings"].Value, CultureInfo.InvariantCulture);
     decimal gdsNumberPercent = decimal.Parse(m.Groups["percent"].Value, CultureInfo.InvariantCulture);
     string locator = m.Groups["locator"].Value;

     Console.WriteLine("Date:  " + date + ", Locator: " + locator + "Dollar Savings: $" + gdsNumberSavings + " Percent Savings: " + gdsNumberPercent + " % " + "\n");
     gdsTotalSavings += gdsNumberSavings;
     gdsTotalPercentage += gdsNumberPercent;
}

Output: 输出:

Date:  5/21/2015 11:55:56 PM, Locator: NXRMN5Dollar Savings: $257,18 Percent Savings: 11,55 % 

Or, you can use the same logic in a single regex: 或者,您可以在单个正则表达式中使用相同的逻辑:

Match m = Regex.Match(line, @"^(?<date>[^|]+)\|.*Processed\s+(?<locator>[^.]+)\.{3}.*?Savings\s+found:\s*\$(?<savings>\d+\.\d+)\s*\(\s*(?<percent>\d+\.\d+)\s*%");

And inside the if block: if块中:

string date = m.Groups["date"].Value.Trim();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM