Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex expression after quotation on python

Tags:

python

regex

I am trying to develop a program on Python that would get the name of the artists from a twit from Pandora. Like for example if I have this twitter:

I'm listening to "I Can Make It Better" by Luther Vandross on Pandora #pandora http://t.co/ieDbLC393F.

I would like to get only the name Luther Vandross back. I do not know much about regex, so I tried to do the following code:

print  re.findall('".+?" by [\w+]+',  text)    

But the result was "I can Make it Better" by Luther

Do you have any idea on how I would be able to develop a regular expression on python to get it?

like image 411
Filipe Avatar asked Sep 08 '26 04:09

Filipe


2 Answers

Your regex is near, but you can change the delimiters to use " by and on. However, you need to use capturing groups by using parentheses.

You can use a regex like this:

" by (.+?) on

Working demo

Regular expression visualization

The idea behind this regex is to capture the content between the " by and on, using a simple nongreedy regex.

Match information

MATCH 1
1.  [43-58] `Luther Vandross`

Code

import re
p = re.compile(ur'" by (.+?) on')
test_str = u"I'm listening to \"I Can Make It Better\" by Luther Vandross on Pandora #pandora http://t.co/ieDbLC393F.\n"

re.search(p, test_str)
like image 94
Federico Piazza Avatar answered Sep 10 '26 07:09

Federico Piazza


>>> s = '''I'm listening to "I Can Make It Better" by Luther Vandross on Pandora #pandora http://t.co/ieDbLC393F.'''

>>> import re
>>> m = re.search('to "?(.*?)"? by (.*?) on #?Pandora', s)
>>> m
<_sre.SRE_Match object; span=(14, 69), match='to "I Can Make It Better" by Luther Vandross on P>
>>> m.groups()
('I Can Make It Better', 'Luther Vandross')

More test cases:

>>> tests = [
    '''I'm listening to "Don't Turn Out The Lights (D.T.O.T.L.)" by NKOTBSB on #Pandora''',
    '''I'm listening to G.O.D. Remix by Canton Jones on #Pandora''',
    '''I'm listening to "It's Been Awhile" by @staindmusic on Pandora #pandora http://pdora.co/R1OdxE''',
    '''I'm listening to "Everlong" by @foofighters on #Pandora http://pdora.co/1eANfI0''',
    '''I'm listening to "El Preso (2000)" by Fruko Y Sus Tesos on #Pandora http://pdora.co/1GtOHC1'''
    '''I'm listening to "Cat Daddy" by Rej3ctz on #Pandora http://pdora.co/1eALNpc''',
    '''I'm listening to "Space Age Pimpin'" by 8 Ball & MJG on Pandora #pandora http://pdora.co/1h8swun'''
]
>>> expr = re.compile('to "?(.*?)"? by (.*?) on #?Pandora')
>>> for s in tests:
        print(expr.search(s).groups())

("Don't Turn Out The Lights (D.T.O.T.L.)", 'NKOTBSB')
('G.O.D. Remix', 'Canton Jones')
("It's Been Awhile", '@staindmusic')
('Everlong', '@foofighters')
('El Preso (2000)', 'Fruko Y Sus Tesos')
("Space Age Pimpin'", '8 Ball & MJG')
like image 27
poke Avatar answered Sep 10 '26 07:09

poke



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!