Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

python - Extract strings that start with ${ and end with }

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'${S+}', 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that your regexp is buggy, and doesn't match the strings you want to extract. Specifically, you have two issues:

  1. { and } are regexp metacharacters, just like $, and also need to be escaped if you want to match them literally.
  2. 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'${S+}', line)

to:

matches = re.findall(r'${S+}', line)

and it should work.

See the Python regular expressions documentation for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...