I have a string, which contains a specific number that I would like extracted into a single number.
string = Result: ResultSet({'(u'examplemeasure', None)': [{u'value': 15, u'time': u'2018-03-20T22:50:33.803894733Z'}]})
I would like the number 15 by itself as a result.
15 (<-- just like this)
My code is as follows.
import re
m = re.search('(\d+)', 'Result: ResultSet({'(u'examplemeasure', None)': [{u'value': 15, u'time': u'2018-03-20T22:50:33.803894733Z'}]})', re.IGNORECASE)
print (m.group(1))
However, the abundance of apostrophe's give a syntax error.
File "filename.py", line 3
print (m.group(1))
^
SyntaxError: invalid syntax
Is there a way to disregard the abundance of apostrophe's and get the number 15 by itself?
Thanks, whyiamafool
The single quotation marks in your ResultSet
needs to be escaped. Or wrap the ResultSet
with double quotation marks.
I figured out how to extract a specific number out of a string with multiple numbers within a few minutes of OP.
You can remove the initial and last apostrophe marks from the string, as shown.
m = re.search('(\d+)', Result: ResultSet({'(u'examplemeasure', None)': [{u'value': 15, u'time': u'2018-03-20T22:50:33.803894733Z'}]}), re.IGNORECASE)
Replacing those apostrophe marks with quotation marks ("") around the string will solve the problem.
m = re.search('(\d+)', "Result: ResultSet({'(u'examplemeasure', None)': [{u'value': 15, u'time': u'2018-03-20T22:50:33.803894733Z'}]})", re.IGNORECASE)
The printed result of this program is...
15
(full code below)
import re
m = re.search('(\d+)', "Result: ResultSet({'(u'examplemeasure', None)': [{u'value': 15, u'time': u'2018-03-20T22:50:33.803894733Z'}]})", re.IGNORECASE)
print (m.group(1))
15
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.