[英]Get list from string using Regex Matches[C#]
如何獲取某個字符串中的日期列表? 如何在這里使用Regex?
示例:“ Lorem aaaa 12.01.2019 ffffffffdddddd hhhhhh 14.01.2019 nnnnnn ggg 15.01.2019 cxcccc...。”
第二個問題:如何學習使用Regex? 您能建議一些鏈接嗎?
var list = Regex.Matches("Lorem aaaa 12.01.2019 ffffffffdddddd hhhhhh 14.01.2019 nnnnnn ggg 15.01.2019 cxcccc ....", @"\\d{2}.\\d{2}.\\d{4}");
我也建議您在Visual Studio中使用此功能,這非常方便。 https://marketplace.visualstudio.com/items?itemName=AndreasAndersen.RegularExpressionTesterExtension
您首先需要編寫一個模式,以匹配提供的字符串中的日期。
class Program
{
public static void Main(string[] args)
{
List<string> dates = new List<string>();
string pattern = @"\b\d{2}.\d{2}.\d{4}\b";
Regex rgx = new Regex(pattern);
var sentence = "Lorem aaaa 12.01.2019 ffffffffdddddd hhhhhh 14.01.2019 nnnnnn ggg 15.01.2019 cxcccc ....";
var matches = rgx.Matches(sentence);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
Console.ReadLine();
}
}
輸出:
以下是一些您可以學習構建自己的正則表達式的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.