shutterpp
shutterpp

Reputation: 65

Python Regex: How to split found string into list elements?

I've been trying to split the string i find with regex into list elements, since re.findall returns a list. This is my source string:

vars.duh["1"] = {
   oid_1 = "1"
   oid_2 = "2"
   oid_3 = "3"
}

This is what i got so far:

regex = re.compile(r"(?<=vars\.duh\[\"1\"\] = {)[^}]*(?=})")
return re.findall(regex, string)

This is the output:

['\n    oid_1 = "1"\n    oid_2 = "2"\n    oid_3 = "3"\n']

It seems like 1 single list element gets returned, which is obvious, since the regex matches this one string. Is it possible to split by \n in the regex, before the list is created, so the list returns multiple elements like so:

['    oid_1 = "1"','    oid_2 = "2"','    oid_3 = "3"']

The number of different oids is dynamic, so i'm afraid i can't work with capture groups, can i? Is there a python or regex way to handle this? cheers

Upvotes: 2

Views: 97

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626853

You may pip install regex and then use

import regex
s = r"""vars.duh["1"] = {
   oid_1 = "1"
   oid_2 = "2"
   oid_3 = "3"
}"""
rx = regex.compile(r'(?<=vars\.duh\["1"] = {[^}]*?)[^}\n]+(?=[^}]*})')
print (rx.findall(s))
# => ['    oid_1 = "1"', '   oid_2 = "2"', '   oid_3 = "3"']

See the Python demo

Regex details

  • (?<=vars\.duh\["1"] = {[^}]*?) - a location immediately preceded with vars.duh["1"] = { string and then any 0 or more chars other than } as few as possible
  • [^}\n]+ - 1 or more chars other than } and a newline
  • (?=[^}]*}) - a location immediately followed with any 0 or more chars other than } as many as possible and then }.

Upvotes: 1

Related Questions