I am struck in writing regular expression for below format XXXXXXG0-XXXX-XXXX-1923-785FEABCD128 Above format is to filter MAC Address, so i need those MAC ADDRESS which has the characters defined in the above format and length
Is it possible to write regexp for above format? X characters can be alphanumeric. But other non X characters should be same.
ABCDEFG0-GHYD-SDER-1923-785FEABCD128 - Valid
ABCDEFH0-GHYD-SDER-0923-995FEABCD120 - Invalid
ABCDEFG0-GHYD-SDER-0923-995FEABCD120 - Invalid
CodePudding user response:
import re
patt = re.compile('[A-Z0-9]{6}G0-[A-Z0-9]{4}-[A-Z0-9]{4}-1923-785FEABCD128')
for test in ['ABCDEFG0-GHYD-SDER-1923-785FEABCD128', 'ABCDEFH0-GHYD-SDER-0923-995FEABCD120', 'ABCDEFG0-GHYD-SDER-0923-995FEABCD120']:
if patt.match(test):
print(f'{test} - Valid')
else:
print(f'{test} - Invalid')
prints
ABCDEFG0-GHYD-SDER-1923-785FEABCD128 - Valid
ABCDEFH0-GHYD-SDER-0923-995FEABCD120 - Invalid
ABCDEFG0-GHYD-SDER-0923-995FEABCD120 - Invalid
CodePudding user response:
^[a-zA-Z0-9]{6}G0-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-1923-785FEABCD128$
Explanation:
^
matches beginning of the string
[a-zA-Z0-9]{6}
matches any alphanumeric character 6 times
G0-
matches that text exactly
[a-zA-Z0-9]{4}-
any alphanumeric character 4 times followed by a hyphen (appears twice)
1923-785FEABCD128
matches that text exactly
$
matches the end of the string