Home > Mobile >  python regex get number and paragraph between number
python regex get number and paragraph between number

Time:09-18

I have a string like below.

10. Title text
text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2


text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2

text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2

11. Title text
text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2


text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2

12. Title text
text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2

13. Title text
text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2


text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2


text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2

What I want to do is to separate the title and content in chunks and put them in a list.

result = [10. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2, 11. Title text\ntext1text2text1text2text1text2text1text2text1text2text1text2text1 text2text1 text2text1 text2text1 text2text1text2\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2, ........]

I've tried this, but honestly I have no idea what to do. help

la_text = []
num = 1
for a in range(3):
    sepa = re.findall(r"\d*(.*)\d*", text)[num]
    la_text.append(sepa)
    num  = 1

print(la_text)

CodePudding user response:

To get the title and paragraph separated, you can make use of a negative lookahead with only the multiline flag re.M

Using re.findall will return a list of tuples with 2 values for the capture groups

^(\d \..*)((?:\n(?!\d \.).*)*)

See a regex demo.

To get them together as a single match:

^\d \..*(?:\n(?!\d \.).*)*

See another regex demo.

import re

pattern = r"^\d \..*(?:\n(?!\d \.).*)*"

s = "...."

print(re.findall(pattern, s, re.M))

Output

[
'10. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n',
'11. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n',
'12. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n',
'13. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2'
]

CodePudding user response:

If s contains your string from the question you can do:

import re

pat = re.compile(r"^(\d \.\s .*?)(?=\n^\d \.|\Z)", flags=re.M | re.S)

print(pat.findall(s))

Prints:

[
    "10. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n",
    "11. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n",
    "12. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n",
    "13. Title text\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n",
]

CodePudding user response:

If you don't need to separate titles from paragraphs, another idea is to use re.split

re.split(r"\n\s*(?=\d \.)", test_str)

See this demo at regex101 or a Python demo at tio.run

  • \n\s* this splits at a newline and any amount of whitespace
  • (?=\d \.) if followed by one or more digits and a period

CodePudding user response:

In case you need a dictionary with numbers as keys:

import re
m = re.findall(r'^(\d ).(.*)((?:\n(?!\d \.).*)*)',s , re.M )
 
{element[0]:[element[1], element[2]] for element in m }

Output:

{'10': [' Title text',
  '\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n'],
 '11': [' Title text',
  '\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n'],
 '12': [' Title text',
  '\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n'],
 '13': [' Title text',
  '\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n\n\ntext1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2text1 text2\n']}
  • Related