I'm analysing files by name.
Example filename to exclude:
Kickloop [124].wav
Example filename to include:
Boomy [Kick].wav
My code currently ignores all file names including square brackets.
def contains_square_brackets(file):
if ("[" in file) and ("]" in file):
return True
Question: Is there a regex way of achieving what I am after?
The regex r'\[\d+\]' will help you. When used correctly it will identify strings containing square brackets surrounding one or more digits.
Example:
>>> import re
>>> def has_numbers_in_square_brackets(s):
... return bool(re.search(r'\[\d+\]', s))
...
>>> has_numbers_in_square_brackets('Hello')
False
>>> has_numbers_in_square_brackets('Hello[123]')
True
>>> has_numbers_in_square_brackets('Hello[dog]')
False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With