Search for quotes with regular expression

I am looking for a way to search for a text file for quotes made by the author and then print them. My script so far:

import re

    #searches end of string 
    print re.search('"$', 'i am searching for quotes"')

    #searches start of string 
    print re.search('^"' , '"i am searching for quotes"')

What i would like to do

import re

## load text file
quotelist = open('A.txt','r').read()

## search for strings contained with quotation marks
re.search ("-", quotelist)

## Store in list or Dict
Dict = quotelist

## Print quotes 
print Dict

I also tried

import re

buffer = open('bbc.txt','r').read()

quotes = re.findall(r'.*"[^"].*".*', buffer)
for quote in quotes:
  print quote

# Add quotes to list

 l = []
    for quote in quotes:
    print quote
    l.append(quote)
0
source share
2 answers

Design a regex that matches all the expected characters you expect to see inside the quoted string. Then use a python method findallto re, to find all occurrences of the match.

import re

buffer = open('file.txt','r').read()

quotes = re.findall(r'"[^"]*"',buffer)
for quote in quotes:
  print quote

A search between "and" requires a search in the form of unicode-regex, for example:

quotes = re.findall(ur'"[^\u201d]*\u201d',buffer)

And for a document that uses "and" interchangeably to complete a quote

quotes = re.findall(ur'"[^"^\u201d]*["\u201d]', buffer)
+2
source

. Python :

>>> haystack = 'this is the string to search!'
>>> needle = '!'
>>> if needle in haystack:
       print 'Found', needle

-

>>> matches = []

...

>>> matches.append('add this string to matches')

, . !

...

l = []
for quote in matches:
    print quote
    l.append(quote)
-2

All Articles