regex pattern matching examples

Regex Pattern Matching Examples

Code # 01

Pakistan Mobile Number Pattern

import re
pattern = r'^[+92]+[3]+[0-4]+[0-9]{8}$'
text = "+923212345678"
m=re.match(pattern,text)
if m:
    print("Pattern Matched")
else:
    print('Pattern Not Match')

This code is checking whether the given phone number is a valid Pakistan phone number. The pattern is defined as follows:

  1. The phone number must start with "+92" ,
  2. After +93 digit must be either 3.
  3. The third digit is Sim Code must be any numeric digit from 0 to 4.
  4. The remaining eight digits must be any numeric digit from 0 to 9.

The given phone number is "+923212345678", which should match the pattern. The re.match() function is used to match the pattern against the given text. If the pattern matches, the code will print "Pattern Matched". Otherwise print "Pattern Not Match".

Code # 02:

Pakistan ID Card Pattern

import re
pattern = r'^[0-9]{5}[-]+[0-9]{7}[-]+[0-9]{1}$'
text = "12345-1234567-1"
m=re.match(pattern,text)
if m:
    print("Pattern Matched")
else:
    print('Pattern Not Match')

This is a Python code snippet that uses the re module to check whether a given string matches the pattern for a Pakistani National Identification Card (NIC) number.The pattern is defined as follows:

  1. The string must start with exactly 5 digits, represented by the character class [0-9]{5}.
  2. This must be followed by a hyphen, represented by the character class [-]+.
  3. The next 7 characters must be exactly digits, represented by the character class [0-9]{7}.This must be followed by a hyphen [-]+
  4. The last character must be exactly 1 digit, represented by the character class [0-9]{1}.
If the input string matches this pattern, the program prints "Pattern Matched"Otherwise print "Pattern Not Match".

Note: that while this pattern matches strings that have the correct format for a Pakistani NIC number, it does not check whether the given NIC number is actually valid or not, as the check digit calculation algorithm is more complex

Code # 03:

Date Pattern

import re
pattern = r'^(0[0-9]|[12][0-9]|3[01])/(0[0-9]|1[0-2])/[0-9]{4}$'
text = "13/04/2023"
m=re.match(pattern,text)
if m:
    print("Pattern Matched")
else:
    print('Pattern Not Match')

This regular expression pattern matches date strings in the format "DD/MM/YYYY". Here's how the pattern works:

  1. ^ matches the start of the string
  2. (0[0-9]|[12][0-9]|3[01]) matches the day component, which can be "01" to "09", "10" to "29", "30", or "31"
  3. / matches the delimiter between the day and month components
  4. (0[0-9]|1[0-2]) matches the month component, which can be "01" to "09" or "10" to "12"
  5. / matches the delimiter between the month and year components
  6. [0-9]{4} matches the year component, which consists of four digits
  7. $ matches the end of the string

So the pattern as a whole matches a string that consists of a valid date in the specified format. In your code snippet, the pattern is used to match the date string "13/04/2023", and since it matches the pattern, the output will be "Pattern Matched". Otherwise print "Pattern Not Match".

For more Information 👇

LIKE SHARE AND SUBSCRIBE ðŸ‘ˆðŸ‘‡ðŸ‘‡ðŸ‘‡