python-regex

Regex pattern to match a string ends with multiple charachters


I want to match a pattern that starts with $ and ends with either dot(.) or double quote("). I tried with this

re.findall(r"\$(.+?)\.",query1) 

Above works for starting with $ and ending with . How to add OR in ending characters so that it matches with pattern ending either with . or with "

Any suggestions ?


Solution

  • The regex pattern you want is:

    \$(.+?)[."]
    

    Your updated script:

    query1 = "The item cost $100.  It also is $not good\""
    matches = re.findall(r"\$(.+?)[.\"]", query1)
    print(matches)  # ['100', 'not good']