0

I am trying to find all the matches from a string using re in python.

my string is of the form

[Page]
# asdf
[/Page]

[Page]
# Hello
## Woo
[/Page]

and I want to find all the contents within the page tags. So currently I have tried -

page = re.compile( 
         r'^\[Page\]' 
         r'([\S\s]*)' 
         r'\[\/Page\]' 
         , re.IGNORECASE 
     )

But doing re.match using this expression matches the first [Page] and the last [/Page]. However I need all such occurrences. Eg.

m = re.match(page, my_string)
m.group(1)

# asdf
[/Page]
[Page]
# Hello
## Woo

I need to match all the Page tags.

Shahrukh Mohammad
  • 985
  • 2
  • 17
  • 33
  • 1
    `[\S\s]*` is greedy, `[\S\s]*?` is not. Use `page = re.compile(r'^\[Page]([\S\s]*?)\[/Page]', re.IGNORECASE | re.MULTILINE)` if `[Page]` should only be matched at the start of a line. Use `re.findall` to get all occurrences (remove `(` and `)` if you need to get all matched text). – Wiktor Stribiżew Mar 30 '19 at 13:18
  • @WiktorStribiżew it worked, thanks for the explanation. – Shahrukh Mohammad Mar 30 '19 at 13:30

0 Answers0