Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...