Chiffa
Chiffa

Reputation: 1506

A regex for a list

I'm using python and re. I have a string that looks like this:

[AddrPart(value='Something cyrillic', type='something'), AddrPart(value='Something else', type=None), ...]

I have a regular expression that can capture the whole group:

r"\[AddrPart(.+)\]\t"

and another one, that captures a part of it but then fails:

\[((AddrPart\(value=[-а-яА-Яё']{1,20}), type=(None|.{1,}))\)\]\t

This one gets everything from the first type to the end of the string before the \)\].

What I want is to get all the AddrParts in the list. I feel like I'm missing something basic and obvious. What might that be?

Upvotes: 1

Views: 48

Answers (1)

Gilad Green
Gilad Green

Reputation: 37281

By default regex returns greedy matches (largest possible match) and you want it lazy. Add ? to the part between the ():

(AddrPart\(.*?\))
# Match 1: "AddrPart(value='Something cyrillic', type='something')"
# Match 2: "AddrPart(value='Something else', type=None)"

If you want it without the outer part of the AddrPart then:

AddrPart\((.*?)\)

To get both the value and type you can add a bit more:

AddrPart\(value=(?<value>.+?),\s*type=(?<type>.+?)\)
# Match 1: Group `value`: 'Something cyrillic', Group `type`: 'something'
# Match 2: Group `value`: 'Something else', Group `type`: None

Upvotes: 2

Related Questions