Python regex: matching multi-point floating point values ​​between brackets

Matches multi-point floating point values ​​between parentheses

In the examples below, I want to extract all floating point values ​​between parentheses belonging only to "group1" using regex, but not values ​​from other groups ("group2", "group3", etc.). The requirement is that this is done via a regular expression in python. Is this possible with regex?


Attempted regex patterns:

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

  • Corresponds to every float value in all groups: ([+-]*\d+\.\d+),

  • Matches without value in any groups: group1 = \[ ([+-]*\d+\.\d+), \]


What should I do to make this work? Any suggestions would be greatly appreciated!


Sample 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,
]

      

+3


source to share


2 answers


Here's the 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]

      

+1


source


Try the following:

\bgroup2 = \[([\s+\d+.\d+[,-\]]+)

      



This is probably not the most optimized solution, but I did it in just a few minutes using this site. http://www.regexr.com/

This is by far the best resource I have found for building regular expressions. It has great examples, links, and a cheat sheet. Paste in the example text and you can tweak the regex and see it update in real time. Hover your cursor over the expression and it will give you detailed information about each part.

0


source







All Articles