需要正则表达式逻辑来查找字符串

我想从一个句子中找到一个特定的字符串(单词)。

给定字符串:

“在给定的健康计划中,您的计划名称:Medical 和计划类型:PPO,其生效日期:2019-01-01 以及承保价值 100 美元和 200 美元”。

  1. 如果我通过"Plan Name:"那么我的输出将是"Medical"

  2. 如果我通过"Plan Type:"那么我的输出将是"PPO"

  3. 如果我通过"effective date:"那么我的输出将是"2019-01-01"

  4. 如果我通过"coverage value"那么在这种情况下我需要两个值。最小值"$100"和最大值"$200"

同样,我需要给定句子中的电子邮件地址。在某些情况下,我只需要从给定的句子中提取日期、电子邮件或数值。在这种情况下,我没有任何以前的值可以匹配。

我需要一个涵盖上述所有要求的正则表达式逻辑。


阿波罗的战车
浏览 110回答 1
1回答

慕丝7291255

尝试以下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace ConsoleApplication118{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string input = "In a given health plan your Plan Name: Medical and Plan Type: PPO whose effective date: 2019-01-01 and coverage value $100 and $200";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string pattern = @"(?'key'\w+):\s+(?'value'[-\d\w]+)|(?'key'\w+)\s+(?'value'\$\d+\s+and\s+\$\d+)";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MatchCollection matches = Regex.Matches(input, pattern);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dictionary<string, string> dict = matches.Cast<Match>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToDictionary(x => x.Key, y => y.FirstOrDefault());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Match match in matches)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Key : '{0}', Value : '{1}'", match.Groups["key"].Value, match.Groups["value"].Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP