I need a regular expression for re.findall that returns anything between ‘$’ signs:
Sentence = "The person, $John Doe$, works at $Lan Tech$ as a $Sales Engineer$."
Printing should produce =
['John Doe', 'Lan Tech', 'Sales Engineer']
Note: I could use something else other than ‘$’ if it would be easier.
Use
re.findall()with a capturing group:Example:
Explanation: You need to escape each
$in the regex, because an unescaped$acts as an end of string anchor in regex. The group is everything inside of the parentheses, andre.findall()will only return the contents of groups if you have any, which is why the$s are not included in the resulting list. Inside of the group,[^$]*will match any number of characters that are not$.