2

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.

3
  • Try reading this page: regular-expressions.info/reference.html from start to finish Commented May 23, 2011 at 7:01
  • @Vit is this task about "extracting" values from the delimited string or validating the delimited string? Commented Jul 3, 2024 at 12:06
  • @mickmackusa I asked this 13 years ago. I have absolutely no idea. Commented Aug 5, 2024 at 23:39

6 Answers 6

8

Here's the shortest I can make it, based on your (somewhat incomplete) specifications:

preg_match('/[a-z\d]{6}(?:_[a-f\d]{32}){2}(?:_\d+){3}/i', $string);

This will match exactly six instances of a letter or digit; followed by an underscore and 32 hexadecimal digits, twice; followed by an underscore and any number of digits, 3 times. The /i at the end puts it into case-insensitive mode.

Sign up to request clarification or add additional context in comments.

Comments

4

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 );
}
?>

2 Comments

Works great, except if I add letters in between number in the last three sections, it still comes up positive if I don't return the values into $match. Either case, this is sufficient for what I needed. Thanks!
@Vitality Isikov That is strange. I can not explain it. You're welcome!
3
$string = "0hv05d_1a8198c8c430c2333fd6e49863f59f60_d41d8cd98f00b204e9800998ecf8427e_3600_3_25";
if (preg_match('/^([a-z0-9]{6})_([a-f0-9]{32})_([a-f0-9]{32})_([0-9]+)_([0-9]+)_([0-9]+)$/i', $string, $matches))
{
    echo "Match!<br />\n";
    echo "First: ".$matches[1]."<br />\n";
    echo "MD5 hashes: ".$matches[2]. " - ".$matches[3]."<br />\n";
    echo "numbers: ".$matches[4]. " - ".$matches[5]. " - ".$matches[6]."<br />\n";
}

notes:

  • md5 are just [a-f0-9], not [a-z0-9].
  • I put /i to case insensitive matches, instead of specifying [a-zA-Z] in every part.

2 Comments

I've edited it several times, if still don't work, substitute \d by [0-9] (I'm editing it now)
I've put that the whole text you are trying to match is the pattern you said. If you need to match that pattern inside a larger text, please remove the ^ at the beginning of the pattern and the $ at the end.
1

Edit- To be more complete you could do this:

if (preg_match('/[a-z0-9]{6}_[a-f0-9]{32}_[a-f0-9]{32}_\d+_\d+_\d+/i', $subject)) {
    # Successful match
} else {
    # Match attempt failed
}

I also to make it more concise replased [0-9] with the \d class, changed the matching of MD5 to just [a-f0-9], and I also changed [a-zA-Z0-9] to [a-z0-9] and included the /i for case insensitive.

If you really wanted to the expression could also be rewritten as:

/[a-z0-9]{6}(?:_[a-f0-9]{32}){2}(?:_\d+){3}/i

Comments

0

[a-zA-Z0-9]{6}_[a-f0-9]{32}_[a-f0-9]{32}_[0-9]+_[0-9]+_[0-9]+

ref: http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

also, since php using PCRE (Pearl Compatible Regular Expressions) you can use this nifty cheat sheet: http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

Comments

0

Validation + Extraction

Strict, full string matching into capture groups using PCRE syntax and producing a string array. Demo

preg_match(
    '/^([a-z\d]{6})_([a-f\d]{32})_([a-f\d]{32})_(\d+)_(\d+)_(\d+)$/',
    $string,
    $m
);

Strict, full string matching into capture groups using POSIX syntax and producing a string array. Demo

preg_match(
    '/^([[:alnum:]]{6})_([[:xdigit:]]{32})_([[:xdigit:]]{32})_([[:digit:]]+)_([[:digit:]]+)_([[:digit:]]+)$/',
    $string,
    $m
);

Matching by format returning the capture count and producing individual scalar variables (strings and integers). Demo

sscanf(
    $string,
    '%6[a-z0-9]_%32[a-f0-9]_%32[a-f0-9]_%d_%d_%d',
    $one,
    $two,
    $three,
    $four,
    $five,
    $six
);

Matching by format returning a scalar array (strings and integers). Demo

sscanf(
    $string,
    '%6[a-z0-9]_%32[a-f0-9]_%32[a-f0-9]_%d_%d_%d',
)

Text Extraction Only

Simple splitting on the delimiter without any validation and producing a string array: Demo

var_export(explode('_', $string));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.