-1

I am new to regular expression, I want to get the content between 'query:' and ',"' . the example data likes here https://maps-api-ssl.google.com/maps/suggest?q=hello . I tried the regular string like "^query(.*),"$", but it not worked, I don't know why, and how can I deal with the problem?

lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30
  • 2
    This is JSON data, don't parse it with regular expressions. Use Python's [built-in JSON parser](https://docs.python.org/2/library/json.html) – David Robinson May 14 '14 at 14:56
  • But it is not the stand format of json data, I have to re-format it before I can use. – lqhcpsgbl May 14 '14 at 15:00
  • 1
    Indeed you're right; it's not standard JSON. But it looks like you can parse it using YAML, see [here](http://stackoverflow.com/questions/9104930/is-there-any-way-to-make-simplejson-less-strict) (once you've installed the PyYAML library). You'll also need to add spaces after each colon, so your line will look something like `import yaml; import urllib2; data = yaml.load(urllib2.urlopen("https://maps-api-ssl.google.com/maps/suggest?q=hello").read().replace(":", ": "))`. Does that work for you? – David Robinson May 14 '14 at 15:12
  • yaml is the best way to solve my problem, thanks for you advice. – lqhcpsgbl May 15 '14 at 02:17

2 Answers2

0

If you REALLY want to extract text by using regex, try this one:

import re

a_pattern = re.compile("query\:(.+?)\,")
re.findall(a_pattern, 'query:text_you_want_to_extract,')
juankysmith
  • 11,839
  • 5
  • 37
  • 62
0

Try re.split() with " as the delimiter:

re.split('[\"]+','query:"some text here"')

Your output will be something like: ['query:', 'some text here', '']

ginmorris
  • 36
  • 2