-5

I need a regular expression that just separates the opening square bracket[ and closing square bracket ] from the following string

String

[{"some_id":"1"},{"some_id":"3"},{"some_id":"5"},{"some_id":"7"}]

  • 1
    I can't find `[]` in your string. Please edit your questions to better explain your issue. – Seblor Aug 07 '19 at 07:03
  • @Seblor `[]` is right at the start of the string – Krushna Joshi Aug 07 '19 at 07:13
  • 2
    This would be a duplicate of [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions), except you shouldn’t use regex to parse JSON. – Biffen Aug 07 '19 at 07:15
  • @Biffen I don't see how my question is a duplicate of the question you mentioned. – Krushna Joshi Aug 07 '19 at 07:19
  • 1
    What about parsing JSON properly or reading the Learning Regular Expression link above or removing the 1st and last character of the string or posting a [mcve] – Pitto Aug 07 '19 at 07:25
  • 1
    Is this really a string or a list of dicts? Well, you could use `re.search("\[(.*)\]", my_str).group(1)` or `my_str.rstrip("]").lstrip("[")` – palvarez Aug 07 '19 at 07:32

1 Answers1

1

Using regex:

import re
re.search("\[(.*)\]", my_str).group(1)

String methods:

my_str.rstrip("]").lstrip("[")

Converting to Python dict (since it's a valid JSON):

import json
json.loads(my_str)
palvarez
  • 1,172
  • 2
  • 7
  • 16