2

I have the following text for example:

[ABC]something
    foo 25
    bar 20

[DEF]something
    foo 50

.....and other similar text like this

I want to extract the three words from the brackets, foo and bar and the digits so i can get the result of re.findall as something like this [('ABC', 'foo 25', bar 20'), ('DEF', 'foo 50')]

I tried the following pattern, but returns many empty strings in list

\[(\w+)\]|\n\s+(\w+\s\d+)
1
  • Use two expressions or a parser altogether. Commented Jun 9, 2020 at 7:32

1 Answer 1

2

You may use

import re

data = """
[ABC]something
    foo 25
    bar 20

[DEF]something
    foo 50
"""

rx_outer = re.compile(r'''
    ^
    \[(?P<section>[^][]+)\]
    (?P<content>(?:.+[\r\n]?)+)
    ''', re.M | re.X)

rx_inner = re.compile(r'\w+\s+\d+')

result = []
for outer in rx_outer.finditer(data):
    section = outer.group('section')
    values = tuple([value.group(0) for value in rx_inner.finditer(outer.group('content'))])
    result.append((section,) + values)

print(result)

Or - as a list comprehension:

result = [(section,) + tuple([value.group(0) for value in rx_inner.finditer(outer.group('content'))])
          for outer in rx_outer.finditer(data)
          for section in [outer.group('section')]]

print(result)

Bot will yield

[('ABC', 'foo 25', 'bar 20'), ('DEF', 'foo 50')]

See the demo for the "outer" and the inner expressions, the rest is programming logic.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, but that replicated 'ABC'.. i want it to be ('ABC', 'foo 25', 'bar 20') 😅
@user13711798: Ah, one moment then.
Thanks. It worked. But this seems way more advanced for me. I was expecting to obtain results directly from re.findall. I will try to learn this in the meantime. Also sorry, it wont let me upvote :(.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.