开发者

How to use Regex to split data together with extracting pattern value

开发者 https://www.devze.com 2023-01-09 21:05 出处:网络
I have to process text file like that: 2010-04-02 ... ... ... 2010-05-01 ... ... ... It is possible to get values by using Split function:

I have to process text file like that:

 2010-04-02
 ...
 ...
 ...
 2010-05-01
 ...
 ...
 ...

It is possible to get values by using Split function:

Regex.Split(text, @"\d{4}-\d{2}-\d{2}")
开发者_如何转开发

Is there way to get dates and related text below the date?

My output should be array of items (date, text).

Thanks


According to the docs, wrapping the string you're splitting on in () (making it a capture group) will result in these captures being included in the array.

Regex.Split(text, @"(\d{4}-\d{2}-\d{2})")


Consider, instead, DateTime.Parse() and DateTime.ParseExact(). They're very efficient.

There's good reference here and here.

If you must use regular expressions, take a look at this. There are options for matching patterns in multi-line text.

0

精彩评论

暂无评论...
验证码 换一张
取 消