Python check string pattern follows xxxx-yyyy-zzzz
Question:
I am trying to build a check for strings that allows a pattern "xxxx-yyyy-zzzz". The string needs to have three blocks seperated by "-". Each block can contain "a-z", "A-Z", "_" (underscore) and "." (dot).
This is what I got to now:
file_name: str = "actors-zero-This_Is_Fine.exe.yml"
ab = re.compile("^([A-Z][0-9])-([A-Z][0-9])-([A-Z][0-9])+$")
if ab.match(file_name):
pass
else:
print(f"WARNING wrong syntax in {file_name}")
sys.exit()
Output is:
WARNING wrong name in actors-zero-This_Is_Fine.exe.yml
Answers:
What exactly is your question?
This looks alright so far. Your file name
returns the warning because you have not specified the underscores in the third "block".
Instead of ([A-Z][0-9])
. you could use character classes:
Like so: ^([w.]+)-([w.]+)-([w.]+)$
Generally, I found the chapter in Automate The Boring Stuff on regex Pattern matching very concise and helpful:
https://automatetheboringstuff.com/2e/chapter7/
You will find the above table in this chapter also.
If I understand the question correctly, you want 3 groups of alphanumerical characters (plus .) separated by dashes:
Regex for this would be ^([w.]+)-([w.]+)-([w.]+)$
^
matches the start of a string
Then we have 3 repeating groups:
([w.]+)
will match letters, numbers, underscores (w
) and dots (.
) at least one time (+
)
We make 3 of these, then separate each with a dash.
We finish off the regex with a $
to match the end of the string, making sure you’re matching the whole thing.
I am trying to build a check for strings that allows a pattern "xxxx-yyyy-zzzz". The string needs to have three blocks seperated by "-". Each block can contain "a-z", "A-Z", "_" (underscore) and "." (dot).
This is what I got to now:
file_name: str = "actors-zero-This_Is_Fine.exe.yml"
ab = re.compile("^([A-Z][0-9])-([A-Z][0-9])-([A-Z][0-9])+$")
if ab.match(file_name):
pass
else:
print(f"WARNING wrong syntax in {file_name}")
sys.exit()
Output is:
WARNING wrong name in actors-zero-This_Is_Fine.exe.yml
What exactly is your question?
This looks alright so far. Your file name
returns the warning because you have not specified the underscores in the third "block".
Instead of ([A-Z][0-9])
. you could use character classes:
Like so: ^([w.]+)-([w.]+)-([w.]+)$
Generally, I found the chapter in Automate The Boring Stuff on regex Pattern matching very concise and helpful:
https://automatetheboringstuff.com/2e/chapter7/
You will find the above table in this chapter also.
If I understand the question correctly, you want 3 groups of alphanumerical characters (plus .) separated by dashes:
Regex for this would be ^([w.]+)-([w.]+)-([w.]+)$
^
matches the start of a string
Then we have 3 repeating groups:
([w.]+)
will match letters, numbers, underscores (w
) and dots (.
) at least one time (+
)
We make 3 of these, then separate each with a dash.
We finish off the regex with a $
to match the end of the string, making sure you’re matching the whole thing.