user1829627
user1829627

Reputation: 325

How to use regular expression to get date from text

I have a text Summary for rxd7865 November 13, 2012 to November 13, 2012.

I want to fetch only date string like November 13, 2012 from above text.

How I can use regular expressions in PHP to do this?

Upvotes: 1

Views: 1242

Answers (2)

Mihai Stancu
Mihai Stancu

Reputation: 16107

The regex:

preg_match('/(?<the_date>(January|February|March) [0-9]{2}, 20[0-9]{2})/', $string, $matches);
echo $matches[the_date];

The explanation:

() // are called capture groups or matches
(?<name>) // are named capture groups or named matches
| // separator between a list of alternative matches
[] // is a character class
[0-9] // is a character class that allows only characters from 0 to 9
{} // is a repetition specifier
[]{2} // allows the character class to repeat twice

Upvotes: 2

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

With preg_match_all, it uses PCRE syntax

Upvotes: 0

Related Questions