2

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

asked Aug 18, 2020 at 11:10
2
  • You could split the output on a newline? Commented Aug 18, 2020 at 11:12
  • 2
    With PyPi regex module, yes. With re, just split the result with a newline. Commented Aug 18, 2020 at 11:12

1 Answer 1

1

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 }.
answered Aug 18, 2020 at 12:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.