vespino
vespino

Reputation: 1940

Matching string regular expression

I would like to match data from strings like the following:

24.Legacy.S01E08.720p.HDTV.x264-AVS[rarbg]

Colony.S02E09.720p.HDTV.x264-FLEET[rarbg]

The following test almost works but needs some tweaks:

preg_match_all(
    '/(.*?).S([0-9]+)E([0-9]+).(.*?)(.*?)[(.*?)]/s',
    $download,
    $posts,
    PREG_SET_ORDER
);

Upvotes: 0

Views: 173

Answers (4)

sfeldmann
sfeldmann

Reputation: 357

If a part is optional just add some ( ) around it and a ? behind it, like this

//         NAME  SEASON   EPISOE   MEDIUM  OPTIONS
$regex = '/(.+)\.S([0-9]+)E([0-9]+)\.(.+)(\[(.+)\])?/i';

but watch out for changing $match indexes

Array
(
    [0] => 24.Legacy.S01E08.720p.HDTV.x264-AVS[rarbg]
    [1] => 24.Legacy
    [2] => 01
    [3] => 08
    [4] => 720p.HDTV.x264-AVS
    [5] => [rarbg]
    [6] => rarbg
)

if you don't need the rarbg value you can skip the inner ()

//         NAME  SEASON   EPISOE   MEDIUM  OPTIONS
$regex = '/(.+)\.S([0-9]+)E([0-9]+)\.(.+)(\[.+\])?/i';

Upvotes: 0

Jan
Jan

Reputation: 43199

Just write it down then :)

^
(?P<title>.+?)         # title
S(?P<season>\d+)       # season
E(?P<episode>\d+)\.    # episode
(?P<quality>[^-]+)-    # quality
(?P<type>[^[]+)        # type
\[
    (?P<torrent>[^]]+) # rest
\]
$

Demo on regex101.com.

Upvotes: 1

sfeldmann
sfeldmann

Reputation: 357

You should not need the /s modifier, it extends . to match meta chars and line breaks. I would recommend to use the /e modifier to also allow lower case 's01e14' Don't forget to escape the regex chars like . and [ with \. and \[

//         NAME  SEASON   EPISOE   MEDIUM  OPTIONS
$regex = '/(.+)\.S([0-9]+)E([0-9]+)\.(.+)\[(.+)\]/i';
preg_match_all(
    $regex,
    $download,
    $posts,
    PREG_SET_ORDER
);

Test with '24.Legacy.S01E08.720p.HDTV.x264-AVS[rarbg]'

Array
(
    [0] => 24.Legacy.S01E08.720p.HDTV.x264-AVS[rarbg]
    [1] => 24.Legacy
    [2] => 01
    [3] => 08
    [4] => 720p.HDTV.x264-AVS
    [5] => rarbg
)

Upvotes: 1

Neil
Neil

Reputation: 14321

You're so close, you just need to add the tests for the second half of the requirements:

(.*?).S([0-9]+)E([0-9]+).(.*?)-(.*?)\[(.*?)\]

https://regex101.com/r/PfgMfq/1

Upvotes: 1

Related Questions