Optimus
Optimus

Reputation: 2210

How to get the second value using regex

I have two date values in my text file and I want to find the second date using regex my text is given below

From: dummy one . To: dummy two Page:1/2 Date: 12/10/2014

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla at bibendum odio. Aliquam turpis nisl, fermentum a consequat eget, placerat id ante. Etiam non lacus nisl. Nullam id tincidunt elit

name :dummy 3 Date: 12/11/2014

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla at bibendum odio. Aliquam turpis nisl, fermentum a consequat eget, placerat id ante. Etiam non lacus nisl. Nullam id tincidunt elit

I want get the second date 12/11/2014 my current regex expression is given below

(DATE:)\s*(?<PLAYDATE>\d{1,2}[/|-]\d{1,2}[-|/](?:\d{4}|\d{2})).*\r\n

its returns first date I'm a rookie in regex so please help me thanks

Upvotes: 0

Views: 939

Answers (2)

Amit Joki
Amit Joki

Reputation: 59232

Just take the second match. Supposing file is where the text is, you can do

Regex.Matches(file, @"\d{2}/\d{2}/\d{4}/")[1].Value

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

You need to turn on case insensitive modifier i in-order to do a case-insensitive match.

(?is)(DATE:)\s*(?<PLAYDATE>\d{1,2}[/-]\d{1,2}[-/](?:\d{4}|\d{2}))(?!.*\bDate:)

DEMO

(?!.*\bDate:) Asserts that the string following the date won't contain another Date: substring.

Upvotes: 1

Related Questions