I'm trying to extract the strings from a file that start with ${
and ends with }
using Python. I am using the code below to do so, but I don't get the expected result.
My input file looks like this:
Click ${SWIFT_TAB}
Click ${SEARCH_SWIFT_CODE}
and I want to get a list as below:
${SWIFT_TAB}
${SEARCH_SWIFT_CODE}
My current code looks like this:
def findStringFromFile(file):import os,re with open(file) as f:ans = [] for line in f:matches = re.findall(r'\b\${\S+}\b', line)ans.extend(matches) print (ans)
I am expecting a list of strings that start with ${
and end with }
, but all I currently get is an empty list.
The problem is that your regexp is buggy, and doesn't match the strings you want to extract. Specifically, you have two issues:
{
and }
are regexp metacharacters, just like $
, and also need to be escaped if you want to match them literally.
\b
matches a word boundary, i.e. a position between a "word character" (a letter, a number or an underscore) and a "non-word character" (anything else) or the beginning/end end of string. It does not match between, say, a space and $
.
To fix these issues, change your line:
matches = re.findall(r'\b\${\S+}\b', line)
to:
matches = re.findall(r'\$\{\S+\}', line)
and it should work.
See the Python regular expressions documentation for more details.