php-regexHow to match a GUID using regex in PHP?
A GUID (Globally Unique Identifier) is a 128-bit number used to identify resources in a unique way. To match a GUID using regex in PHP, you can use the following code:
$regex = '/^\{?[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}?$/';
if (preg_match($regex, $guid)) {
echo "Matched!";
}
The code above uses the following parts:
$regex: The regex pattern used to match the GUID. It consists of 8 hexadecimal characters, followed by a hyphen, followed by 3 groups of 4 hexadecimal characters, followed by a hyphen, followed by 12 hexadecimal characters.preg_match(): The PHP function used to match the regex pattern against the given string. It takes two parameters: the regex pattern and the string to match against.$guid: The string to match against.
If the string matches the regex pattern, the preg_match() function will return true and the output will be Matched!.
Helpful links
More of Php Regex
- How to use PHP regex to match an exact string?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match UUID?
- How to use PHP regex to match a year?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to match a double quote in PHP regex?
- How to match a quotation mark in PHP regex?
- How to use PHP regex to get a YouTube video ID?
- How to extract a group when using regex in PHP?
See more codes...