I might have pretty basic question about regex. I have the following regex, which when hardcoded int the application work fine, but then I read it with ConfigParser doesn't seem to work:
r"\[[+-]?\d+(?:\.\d+)?\]"
The way how I read it is :
Config = ConfigParser.ConfigParser()
Config.read("test.conf")
test_regex = Config.get("test","test_regex")
search_pattern = re.compile(test_regex)
test_result = search_pattern.findall(text_to_parse)
The part of the test.conf
[test]
test_regex=r"\[[+-]?\d+(?:\.\d+)?\]"
The input for testing might be something as follows:
text_to_parse = " Here is the [TEST-DONE]" // Success: my regex is extracting [TEST-DONE]
text_to_parse = " Here is the some text" // Failure my regex returns empty list
Any solution for this issue?
Thanks a lot,
Serhiy.
EDIT: was my attention error, as I mention in the comment to answer, but the solution of remotion of the r from regex when it's in the file helped a lot.
You can use ast.literal_eval
to parse the string according to the Python rules:
>>> import ast
>>> ast.literal_eval(conf.get("test", "test_regex"))
'\\[[+-]?\\d+(?:\\.\\d+)?\\]'
But it's easier to just change your config file to contain the unescaped regex in the first place:
[test]
test_regex=\[[+-]?\d+(?:\.\d+)?\]
>>> conf.get("test", "test_regex")
'\\[[+-]?\\d+(?:\\.\\d+)?\\]'
That said, the regex doesn't seem to do what you think it does. It matches:
Example:
>>> re.findall(r'\[[+-]?\d+(?:\.\d+)?\]', 'foo [+10] bar [-3.5]')
['[+10]', '[-3.5]']
Of course there will not be any matches in both of your example strings, because they don't contain the pattern!