Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to say that string starts with pattern

Is it possible to write a regex to check that a string starts with:

  • 3 digits
  • either - or +
  • 4 digits
  • either - or +
  • 2 digits

Examples of strings the pattern should match:

  • ‘123-1234-12’
  • ‘123-1234-‘
  • ‘123-1234’
  • ‘123-‘
  • ‘123’

Examples of strings that the pattern should not match:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • ‘1231234-12’
  • ‘123-123412’
  • ‘123-1234-12’
  • ‘123–12’

Here’s some Python code I’ve written which accomplishes the same thing. Just wondering though, it is possible to do this with a regular expression?

import re
def match(string):
    for sep1 in ('-', '+'):
        for sep2 in ('-', '+'):
            if re.search(fr'^\d{{3}}$', string):
                return True
            elif re.search(fr'^\d{{3}}\{sep1}$', string):
                return True
            elif re.search(fr'^\d{{3}}\{sep1}\d{{4}}$', string):
                return True
            elif re.search(fr'^\d{{3}}\{sep1}\d{{4}}\{sep2}$', string):
                return True
            elif re.search(fr'^\d{{3}}\{sep1}\d{{4}}\{sep2}\d{{2}}$', string):
                return True
    return False

expected_pass = [
    '123-1234-12',
    '123-1234-',
    '123-1234',
    '123-',
    '123',
]
for string in expected_pass:
    print('expected pass', match(string))

expected_fail = [
    '123-123412',
    '1231234-12',
    '123--12',
]
for string in expected_fail:
    print('expected fail', match(string))

>Solution :

Try with this one:

^\d{3}(?:[\-+](?:\d{4}(?:[\-+](?:\d{2})?)?)?)?$

Regex Explanation:

  • ^: start of string
  • \d{3}: starts with three digits
  • (?:[\-+] ... )?: optional - or +
  • (?:\d{4} ... )?: further optional 4 digits
  • (?:[\-+] ... )?: even further optional - or +
  • (?:\d{2} ... )?: even more further optional 2 digits
  • $: end of string

Check the regex demo here.


Your Python code would become:

import re

pattern = r'^\d{3}(?:[\-+](?:\d{4}(?:[\-+](?:\d{2})?)?)?)?$'

expected_pass = ['123-1234-12', '123-1234-', '123-1234', '123-', '123']
for string in expected_pass:
    string_match = re.match(pattern, string)
    print('expected pass', string_match)

expected_fail = ['123-123412', '1231234-12', '123--12']
for string in expected_fail:
    string_match = re.match(pattern, string)
    print('expected fail', string_match)

Check the python demo here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading