Extracting string constant from source code in a string using regular expressions in Python [closed]

StackOverflow https://stackoverflow.com/questions/16001544

  •  03-04-2022
  •  | 
  •  

質問

How can I get a string constant from source code in a string?

For example, here is the source code I am trying to process:

var v = "this is string constant + some numbers and \" is also included "

I am unable to get everything inside quotation marks. by using this regular expression: "(.*?)".

I can't get var, v, = or anything else except string character.

役に立ちましたか?

解決

Using lookbehind, to make sure the " is not preceded by a \

import re

data = 'var v = "this is string constant + some numbers and \" is also included "\r\nvar v = "and another \"line\" "'
matches = re.findall( r'= "(.*(?<!\\))"', data, re.I | re.M)
print(matches)

Output:

['this is string constant + some numbers and " is also included ', 'and another "line" ']

他のヒント

You need to match an opening quote, then anything that's either an escaped character or a normal character (except quotes and backslashes), and then a closing quote:

"(?:\\.|[^"\\])*"

For get everything inside quotation marks you can try this: "\".+?\"" with re.findall()

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top