GUID stands for Globally Unique Identifier. It is a 128-bit number used to identify information in computer systems. It is used to uniquely identify objects or records in a database, and is commonly used for authentication, authorization, and security purposes. In this article let’s understand how we can create a regex for GUID and how regex can be matched for a valid GUID.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of a GUID
A IFSC Code should have the following criteria and structure-
- It should be a 128-bit number.
- It should be 36 characters (32 hexadecimal characters and 4 hyphens) long.
- It should be displayed in five groups separated by hyphens (-).
- Microsoft GUIDs are sometimes represented with surrounding braces.
Regex for checking if GUID is valid or not
Regular Expression-
/^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
A123-SF-G44-353ASDSDF-GDS231AFD2 | does not match |
234b7543-a46a-21c6-d422-9AD7ADACBA52 | matches |
{12312345323112312} | does not match |
Here is a detailed explanation of the above regex-
/^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$/gm
^ asserts position at start of a line
Match a single character present in the list below [{]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
{ matches the character { with index 12310 (7B16 or 1738) literally (case sensitive)
Match a single character present in the list below [0-9a-fA-F]
{8} matches the previous token exactly 8 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
1st Capturing Group ([0-9a-fA-F]{4}-){3}
{3} matches the previous token exactly 3 times
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
Match a single character present in the list below [0-9a-fA-F]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
Match a single character present in the list below [0-9a-fA-F]
{12} matches the previous token exactly 12 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
Match a single character present in the list below [}]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
} matches the character } with index 12510 (7D16 or 1758) literally (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to check if the string is a valid IFSC Code or not. In this article, we explored the significance of GUIDs (Globally Unique Identifiers) in computer systems. GUIDs serve as crucial tools for identifying and securing information within databases. We delved into the creation of a regex (regular expression) for GUIDs and how it can be employed to validate the authenticity of GUIDs. Regex, a powerful text manipulation tool, plays a pivotal role in various programming contexts, from searching and pattern matching to data validation.