DebugPointer
Published on

Regex for UPI ID

Regex for UPI ID

UPI (Unified Payments Interface) is a payment system developed by the National Payments Corporation of India (NPCI) that allows bank account holders in India to send and receive money, and make payments for goods and services through a single platform. It allows for real-time, peer-to-peer (P2P) transactions and is built on top of the Immediate Payment Service (IMPS) infrastructure. In this article let's understand how we can create a regex for underscore and how regex can be matched for underscore values.

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.

Conditions to match a UPI ID

  • It can contain only lowercase letters, numbers, and the characters . and -.
  • It must contain the @ character.
  • It should end with a series of lowercase letters.
  • UPI ID should be between 6 to 40 characters long.

Regex for checking if its a valid UPI ID

Regular Expression-

/[\.\-a-z0-9]+@[a-z]+/gm

Test string examples for the above regex-

Input StringMatch Output
zerodoes not match
1does not match
random@yblmatches
hello@upimatches
great@iblmatches

Here is a detailed explanation of the above regex-

/[\.\-a-z0-9]+@[a-z]+/gm

Match a single character present in the list below [\.\-a-z0-9]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
\- matches the character - with index 4510 (2D16 or 558) literally (case insensitive)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
@ matches the character @ with index 6410 (4016 or 1008) literally (case insensitive)
Match a single character present in the list below [a-z]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
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)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

Hope this article was useful to check the validity of an UPI ID using regex.