So I have a yaml file that I'm using as a config file. I'm trying to do some string matching with regular expressions, but I'm having trouble interpreting the regex from yaml into python. The regex in question looks like this:
regex:
- [A-Za-z0-9]
And when I try to use the re.match
function, I get this error:
Traceback (most recent call last):
File "./dirpylint.py", line 132, in <module>
sys.exit(main())
File "./dirpylint.py", line 32, in main
LevelScan(level)
File "./dirpylint.py", line 50, in LevelScan
regex_match(level)
File "./dirpylint.py", line 65, in regex_match
if re.match(expression, item) == None:
File "/usr/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
File "/usr/lib/python2.7/re.py", line 229, in _compile
p = _cache.get(cachekey)
TypeError: unhashable type: 'list'
I understand that it's interpreting the regex as a list, but how would I use the regex defined in the yaml file to search for a string?
The problem is the YAML, not the Python.
If you want to store a string value containing literal square brackets in a YAML file, you have to quote it:
regex:
- '[A-Za-z0-9]'
The use of 'single quotes' means the YAML will not interpret any backslash escape sequences in the regex. e.g. \b
Also, note that in this YAML the value of regex
is a list containing one string, not a simple string.