seiqooq
seiqooq

Reputation: 11

Need help using regex to extract strings from json string from text (python)

Hi so I'm trying to extract moderator names from a simple piece of code like this:

{
  "_links": {},
  "chatter_count": 2,
  "chatters": {
    "moderators": [
      "nightbot",
      "vivbot"
    ],
    "staff": [],
    "admins": [],
    "global_mods": [],
    "viewers": []
  }
}

I've been trying to grab the moderators using \"moderators\":\s*[(\s*\"\w*\"\,)\s*] but to no success. I'm using regex over json parsing mostly for the challenge.

Upvotes: 1

Views: 1039

Answers (1)

engineer14
engineer14

Reputation: 617

moderators = list()
first = re.compile(r'moderators.*?\[([^\]]*)', re.I)
second = re.compile(r'"(.*?)"')

strings = first.findall(string)
for strings2 in strings:
  moderators = moderators + second.findall(strings2)

This should do the trick

The first regular expression extracts everything between 2 square braces. The second regular expression extracts the string from it.

I broke it up into 2 regex expressions for readability and ease of writing

NOW, using the json module, you could do something much easier:

import json
a = json.loads(string)
moderators = a['chatters']['moderators']

Upvotes: 1

Related Questions