Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for getting content between $ chars from a text

Tags:

python

regex

The problem:
I need to extract strings that are between $ characters from a block of text, but i'm a total n00b when it comes to regular expressions.

For instance from this text:
Li Europan lingues $es membres$ del sam familie. Lor $separat existentie es un$ myth.

i would like to get an array consisting of:
{'es membres', 'separat existentie es un'}

A little snippet in Python would be great.

like image 235
Tom Avatar asked Dec 11 '25 04:12

Tom


1 Answers

Import the re module, and use findall():

>>> import re
>>> p = re.compile('\$(.*?)\$')
>>> s = "apple $banana$ coconut $delicious ethereal$ funkytown"
>>> p.findall(s)
['banana', 'delicious ethereal']

The pattern p represents a dollar sign (\$), then a non-greedy match group ((...?)) which matches characters (.) of which there must be zero or more (*), followed by another dollar sign (\$).

like image 189
John Feminella Avatar answered Dec 12 '25 16:12

John Feminella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!