Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove year from a string PHP

Tags:

string

regex

php

I have created a database of cigarette and trading cards. Each set title has a year associated with it. For example, 1943 or 2011. The year is always 4 characters long, but can be anywhere in the string.

Could someone please help me create a regex that will find the year in the string. I tried '/d{4}\b/' but it is failing.

like image 542
Lizard Avatar asked May 20 '11 14:05

Lizard


Video Answer


2 Answers

(19|20)[0-9][0-9]

This will read in only 1900 and 2000 ranged dates.

like image 92
Mech Avatar answered Sep 18 '22 23:09

Mech


Try this one :

/\b\d{4}\b/

it will match 4 digits embeded with non-words

like image 39
Toto Avatar answered Sep 20 '22 23:09

Toto