-2

I m trying the following:

myfile.txt has the following content ,I want to extract data between each 'abc start' and 'abc end' using regular expression in python. thanks for the help abc start 1 2 3 4 abc end 5 6 7 abc start 8 9 10 abc end

expecting a output as 1 2 3 4 8 9 10

  • What did you try (post your code) and why do you want to use regular expressions in the first place? (They are redundant in this case.) – DYZ Dec 16 '18 at 20:20
  • What is the difference from this file and one that only contains the numbers separated by newline? – Edgar Andrés Margffoy Tuay Dec 16 '18 at 20:21
  • Possible duplicate of [Regex Match all characters between two strings](https://stackoverflow.com/questions/6109882/regex-match-all-characters-between-two-strings) – sahasrara62 Dec 16 '18 at 20:28
  • SO is about fixing _your_ Code - not doing your work for you. Please go over [ask] and [help/on-topic] again and if you have questions provide your code as [mcve]. If you encounter errors, copy and paste the error message verbatim (word for word) into your question. – Patrick Artner Dec 16 '18 at 20:29

2 Answers2

1
import re
with open('myfile.txt') as f:
    txt = f.read()
    strings = re.findall('abc start\n(.+?)\nabc end', txt, re.DOTALL)

# to transform to your output..
result = []
for s in strings:
    result += s.split('\n')
print(result)
#['1', '2', '3', '4', '8', '9', '10']
Yosi Hammer
  • 588
  • 2
  • 8
1

using regex

import re

string = ''
with open('file.txt','r') as f:
    for i in f.readlines():
       string +=i.strip()+' ' 
    f.close()
exp = re.compile(r'abc start(.+?)abc end')
result = [[int(j) for j in list(i.strip().split())] for i in exp.findall(string)]

print(result)
# [[1, 2, 3, 4], [8, 9, 10]]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44