tenub
tenub

Reputation: 3446

RegEx To Match Last "Word" Containing Any Number

I'm trying to match the last "word" of a string that contains numbers. Basically, I want to match any characters after the final (space) of the string if any of them are a number.

In my particular case and as an example I'm trying to extract season/episode from a TV show RSS feed. If I have: Show Name 12x4 or Show Name 2013-04-12 I wish to match 12x4 and 2013-04-12.

I started with a last word regex ([^ ]*$) but some shows don't have a episode or date (Show Name) so I wish to make my regex more specific.

Upvotes: 1

Views: 1280

Answers (1)

elclanrs
elclanrs

Reputation: 94101

Try with this regex:

/[\w\-]+$/

Sometimes regex is just not enough, or it gets more complicated... Try splitting the string first:

$str = 'Show Name 12x4';
$lastWord = array_pop(explode(' ', $str));

if (preg_match('/(?=\d)/', $lastWord)) {
  ...
}

Upvotes: 1

Related Questions