0

I need a capturing group that will return me inner1: and inner2: between curly braces but outter:.

outter: value
{ inner1: value, inner2: value, ... }

I tried this one;\{.*?(\w*\:).*\} and I only got the first inner1:. What kind of pattern should I use in order to return the rest of groups between curly braces?

Eren CAY
  • 686
  • 1
  • 7
  • 17

3 Answers3

1

This looks like JSON syntax. why not just serialize it into a JSON object or a dictionary and extract based on the keys?

Lorcan O'Neill
  • 3,303
  • 1
  • 25
  • 24
  • It looks like JSON but it is not and I need a hard-coded regular expression because I have no such option like serializing this codes into a JSON object. – Eren CAY Jan 24 '14 at 15:05
1

This appears to capture inner1: and inner2:

^\{\s*(?:(\w+:)\s*\w+\s*,?\s*)+\s*\}$

EDIT: Changed slightly, it shows it captures the values when I use expresso to test it.

CaffGeek
  • 21,856
  • 17
  • 100
  • 184
  • At first glance I thought it would work but apparently it is only capturing the last one, `inner2`, if I am not mistaken. – Eren CAY Jan 24 '14 at 15:08
  • Updated, I also use Expresso to test, and it captures both.It's the same match, but it should contain multiple values. What language are you running this in? – CaffGeek Jan 24 '14 at 15:40
  • It is Python but right now it does not capture any group at all even though it feels right. – Eren CAY Jan 24 '14 at 16:21
  • @ErenCAY, it's really strange, I can put captures around EVERYTHING, and it still doesn't show the values from the first capture...strange. – CaffGeek Jan 24 '14 at 17:09
0

If your data is well-formed json you can use a json parser.

Another way is to use a simple pattern to extract all the content inside curly brackets {([^}]++)} and split the result.

a full regex way: (work with an undefined number of key/value)

(?>{|\G(?<!\A):[^,}]++,)\s*([^:]++)

the result is in the capture group 1

pattern details:

(?>            # atomic group: all that can be before the key
    {          # literal: {
  |            # OR
    \G(?<!\A)  # contiguous to a precedent match but not a the start of the string 
    :[^,}]++,  # a : followed by all that is not a , or } followed by a ,
)              # close the atomic group
\s*            # possible spaces
([^:]++)       # capture group 1: all that is not a :

example:

text = <<EOF
outter: value
{ inner1: value, inner2: value, inner3: val }
EOF

puts text.scan(/(?>{|\G(?<!\A):[^,}]++,)\s*([^:]++)/)
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125