(regular expression) How to remove quotation and whats inside all together in python?

Viewed 89

I wanted to remove strings in a file (a code file in which all strings are quotations) like this:

text = "Hello,"+Tom+"have a nice day!"
text2 = "Thank"+"you."

I want this (not just quotas but everything that's inside):

['text', 'Tom','text2']

I could achieve getting every string using regular expressions, and its read line by line:

readLine = re.findall("[a-zA-Z0-9]*", line)
# there is some trimming I didn't show

But the result is:

['text','Hello','Tom','have', 'a', 'nice', 'day', 'text2', 'Thank', 'you']

If regex is not suitable, what are other ways? Any help is appreciated.

4 Answers

You can use a positive lookahead in your regex, like this:
[a-zA-z0-9]+(?=( = ))

Use

import re
 
expr = r'"(?:[^"\\]|\\[\s\S])*"|(\w+)'
text = r'''text = "Hello,"+Tom+"have a nice day!"
           text2 = "Thank"+"you."'''
print(list(filter(None,re.findall(expr, text))))

See Python proof

Results: ['text', 'Tom', 'text2']

Regex explanation

--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [^"\\]                   any character except: '"', '\\'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    \\                       '\'
--------------------------------------------------------------------------------
    [\s\S]                   any character of: whitespace (\n, \r,
                             \t, \f, and " "), non-whitespace (all
                             but \n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1

I tried

re.findall(r'".*"',line)

You can simply trim the extra quotes at the beginning and end

Edit: To trim it you can use

[ match[1:-1] for match in re.findall(r'".*"',line) ]

Here you go, that is all you need:

re.findall('"(.*)"', sentence)
Related