I wanted to extract only digit values from the following key value pairs data:
data='''
var char = [
{d:new Date(1631052000000),k:67.200,o:0},
{d:new Date(1631138400000),k:67.830,o:0},
{d:new Date(1631224800000),k:67.420,o:0},
{d:new Date(1631484000000),k:68.070,o:0},
{d:new Date(1631570400000),k:67.630,o:0},
{d:new Date(1631656800000),k:68.430,o:0},
{d:new Date(1631743200000),k:67.500,o:0},
{d:new Date(1631829600000),k:68.770,o:0},
{d:new Date(1632088800000),k:68.880,o:0},];
'''
Expected Output: For example
1631052000000, 67.200, 0
>Solution :
You can use \d+ to match digits. Furthermore, you can use \.\d+ in an optional group to optionally match a dot and digits following this dot.
import re
data='''
var char = [
{d:new Date(1631052000000),k:67.200,o:0},
{d:new Date(1631138400000),k:67.830,o:0},
{d:new Date(1631224800000),k:67.420,o:0},
{d:new Date(1631484000000),k:68.070,o:0},
{d:new Date(1631570400000),k:67.630,o:0},
{d:new Date(1631656800000),k:68.430,o:0},
{d:new Date(1631743200000),k:67.500,o:0},
{d:new Date(1631829600000),k:68.770,o:0},
{d:new Date(1632088800000),k:68.880,o:0},];
'''
# Construct a regex to match digits only
pattern = re.compile("\d+(?:\.\d+)?")
# Find all matches of this pattern
print(pattern.findall(data))
This outputs
['1631052000000', '67.200', '0', '1631138400000', '67.830', '0', '1631224800000', '67.420', '0', '1631484000000', '68.070', '0', '1631570400000', '67.630', '0', '1631656800000', '68.430', '0', '1631743200000', '67.500', '0', '1631829600000', '68.770', '0', '1632088800000', '68.880', '0']
You can further transform this into floats, integers, etc. depending on your need.
The entire regex used in the code is the following:
\d+ # Match 1 or more digits
(?: # Start a non-capturing, optional group
\. # Match a . literally
\d+ # Match 1 or more digits
)? # Close the non-capturing, optional group