Regex
regex
December 13, 20224 min read

Regex for GUID validation

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 StringMatch Output
A123-SF-G44-353ASDSDF-GDS231AFD2does not match
234b7543-a46a-21c6-d422-9AD7ADACBA52matches
12312345323112312does 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.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
November 30, 20222 min read
Next Article
December 13, 20223 min read