phpregexpreg-match

Regex pattern for matching a delimited string


I need a way to match the following pattern:

0hv05d_1a8198c8c430c2333fd6e49863f59f60_d41d8cd98f00b204e9800998ecf8427e_3600_3_25

Explained:

There are 6 sections separated by underscores. First section is always 6 [a-zA-Z0-9] characters. Second and third sections are MD5 hashes, so they will always be 32 [a-zA-Z0-9] characters each. Last 3 sections can only be numbers.

I'm using PHP preg_match() to do this.


Solution

  • You could try:

    <?php
    $string = '0hv05d_1a8198c8c430c2333fd6e49863f59f60_d41d8cd98f00b204e9800998ecf8427e_3600_3_25';
    if (preg_match('/([a-zA-Z0-9]{6})_([a-fA-F0-9]{32})_([a-fA-F0-9]{32})_([0-9]+)_([0-9]+)_([0-9]+)/',$string,$match)) {
        print_r( $match );
    }
    ?>