-2

i have this

begin 
    [someText]
end
  • [someText] is dynamic
  • I want to match only [someText] with a regex .
  • I only want what's between these two words ( begin , end )
Ziyadsk
  • 381
  • 4
  • 8
  • 1
    What did you try so far? Do you want a greedy or non-greedy match? – Håken Lid Nov 10 '18 at 17:36
  • Which language are you using? Please post some example code ideally. How do you want to handle whitespace--do you want it included in the match? – moo Nov 10 '18 at 17:40
  • no Mark , i only want the string inside the two words . how to get all the occurences of string between them ? – Ziyadsk Nov 10 '18 at 17:43
  • 1
    What about `begin begin end begin end end end`? Or are you saying that begin and end are never nested and always at the start of a line? – Jon Clements Nov 10 '18 at 17:46
  • Actually yes , sometimes they are nested , and not always at the start of the line . – Ziyadsk Nov 10 '18 at 17:50

1 Answers1

2

Keeping all whitespace

re.match(r'begin(.*)end', text, re.DOTALL).group(1)

Ignoring whitespace before "begin" and "end":

re.match(r'begin\s*(.*)\s*end', text, re.DOTALL).group(1)

This assumes text always contains a match and begins with "begin".

roeen30
  • 759
  • 3
  • 13