Here's a python-code-snippet:
import re
VARS='Variables: "OUTPUTFOLDER=installers","SETUP_ORDER=Product 4,Product 4 Library","SUB_CONTENTS=Product 4 Library","SUB_CONTENT_SIZES=9364256","SUB_CONTENT_GROUPS=Product 4 Library","SUB_CONTENT_DESCRIPTIONS=","SUB_CONTENT_GROUP_DESCRIPTIONS=","SUB_DISCS=Product 4,Product Disc",SUB_FILENAMES='
comp = re.findall(r'\w+=".*?"', VARS)
for var in comp:
print var
This is the output currently:
SUB_CONTENT_DESCRIPTIONS=","
SUB_CONTENT_GROUP_DESCRIPTIONS=","
However I'd like the output to extract all elements so it looks like this:
"OUTPUTFOLDER=installers"
"SETUP_ORDER=Product 4, Product 4 Library"
"SUB_CONTENTS=Product 4"
"SUB_CONTENT_SIZES=9364256"
...
What is wrong with my regex-pattern?
Use this regex.
comp = re.findall(r'"\w+=.*?"', VARS)
Results:
>>>
"OUTPUTFOLDER=installers"
"SETUP_ORDER=Product 4,Product 4 Library"
"SUB_CONTENTS=Product 4 Library"
"SUB_CONTENT_SIZES=9364256"
"SUB_CONTENT_GROUPS=Product 4 Library"
"SUB_CONTENT_DESCRIPTIONS="
"SUB_CONTENT_GROUP_DESCRIPTIONS="
"SUB_DISCS=Product 4,Product Disc"
In my opinion, you could do this in a more clever way, and store your "vars" in a dictionary.
d = dict(var.strip('"').split('=') for var in re.findall(r'"\w+=.*?"', VARS))
To see the dictionary:
for k, v in d.items():
print k, '=', (v if v else '<NONE>')
Results:
>>>
SETUP_ORDER = Product 4,Product 4 Library
SUB_CONTENT_DESCRIPTIONS = <NONE>
SUB_DISCS = Product 4,Product Disc
SUB_CONTENT_GROUPS = Product 4 Library
SUB_CONTENT_SIZES = 9364256
SUB_CONTENT_GROUP_DESCRIPTIONS = <NONE>
OUTPUTFOLDER = installers
SUB_CONTENTS = Product 4 Library
Use this regex:
r'"\w+?=.*?"'
The difference between my and your regexes, see for yourself:
r'"\w+?=.*?"' # mine
r'\w+=".*?"' # your's
Just one "
.
Output:
>>> regex = re.compile(r'"\w+?=.*?"')
>>> regex.findall(string)
[u'"OUTPUTFOLDER=installers"', u'"SETUP_ORDER=Product 4,Product 4 Library"',
u'"SUB_CONTENTS=Product 4 Library"', u'"SUB_CONTENT_SIZES=9364256"',
u'"SUB_CONTENT_GROUPS=Product 4 Library"', u'"SUB_DISCS=Product 4,Product Disc"']
You could try this:
comp = re.findall(r'"(.*?)"', VARS)
print [x for x in comp]
Roughly you are getting whatever comes within the double quotes in a non greedy manner.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.