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:
- '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))
CodePudding user response:
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.