Regex expression

I’m trying to create a regex to match the productCode of this table


So, near as I can tell.l…
Its an 8 characters in length, starts with an S, then a two digit number between 00 and 20, then a _, followed by 4 digits
Hows this?

//Grab the GET & test it using regex.
$code = $_GET['Code'];

$pattern = '/^S[0-20]_\d{8}/';
	if (preg_match($pattern, $code)) {
		
       echo "<h1>Product Found</h1>";

	} else {

	echo '<h3>';
	echo 'Where did that product come from, cause we dont have it.';
	echo '</h3>';
	}

how far am I off?

i think the [0-20] will match 0, 1, 2, 0 because the range interpretes only single characters, not the actual value. the {8} is matched on your last phrase only, so the complete regex would be

^S\d{2}_\d{4}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.