Home > OS >  Extract text between quotation using regex python
Extract text between quotation using regex python

Time:10-13

I have tried extracting text inside quotations ""

file content:
"abc"
"ABC. XYZ"
"1 - 2 - 3"

code i've tried using regex

title = re.findall(r'\"(. ?)\"', filecontent)
print(title)

Output:

['abc']
[] # Some lines comes out like this empty
['1 - 2 - 3']

Some of the lines comes empty not sure why. is there an alternative better way to do this?

CodePudding user response:

If you want to extract some substring out of a string, you can go for re.search.

Demo:

import re

str_list = ['"abc"', '"ABC. XYZ"', '"1 - 2 - 3"']

for str in str_list:
    search_str = re.search('"(. ?)"', str)
    if search_str:
        print(search_str.group(1))

Output:

abc
ABC. XYZ
1 - 2 - 3

Updated as per the valuable comment from The fourth bird:

(You don't have to escape the double quote)

CodePudding user response:

IIUC, Do you try this?

filecontent = '''
"abc"
"ABC. XYZ"
"1 - 2 - 3"
'''

re.findall(r'\"(. ?)\"', filecontent)

Output:

['abc', 'ABC. XYZ', '1 - 2 - 3']
  • Related