Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex python: match multi-line float values between brackets

Match grouped multi-line float values between brackets

In the below example data, I want to extract all the float values between brackets belonging only to "group1" using regex, but not the values from other groups ("group2", "group3" etc.). A requirement is that it is done via regex in python. Is this possible with regex at all?


Regex patterns attempts:

I tried the following patterns, but they capture either everything or nothing:

  1. Matches every float value in all groups: ([+-]*\d+\.\d+),
  2. Matches no value in any groups: group1 = \[ ([+-]*\d+\.\d+), \]


What should I do to make this work? Any suggestions would be very welcome!


Example data:

group1 = [
 1.0,
 -2.0,
 3.5,
 -0.3,
 1.7,
 4.2,
]


group2 = [
 2.0,
 1.5,
 1.8,
 -1.8,
 0.7,
 -0.3,
]


group1 = [
  0.0,
  -0.5,
  1.3,
  0.8,
  -0.4,
  0.1,
]
like image 920
JodyK Avatar asked Mar 20 '26 04:03

JodyK


1 Answers

Here's a regex I created r'group1 = \[\n([ *-?\d\.\d,\n]+)\]':

import re

s = '''group1 = [
 1.0,
 -2.0,
 3.5,
 -0.3,
 1.7,
 4.2,
]


group2 = [
 2.0,
 1.5,
 1.8,
 -1.8,
 0.7,
 -0.3,
]


group1 = [
  0.0,
  -0.5,
  1.3,
  0.8,
  -0.4,
  0.1,
]'''

groups = re.findall(r'group1 = \[\n([ *-?\d\.\d,\n]+)\]', s)
groups = [float(f) for l in map(lambda p: p.split(','), groups) for f in l if f.strip()]
print(groups)

Output:

[1.0, -2.0, 3.5, -0.3, 1.7, 4.2, 0.0, -0.5, 1.3, 0.8, -0.4, 0.1]
like image 107
Taku Avatar answered Mar 21 '26 18:03

Taku