-2

The regex in my regex101 works correctly, but it returns None in my python script. Can't figure out why.

import re

md = """# Some title here


- Show: Show name here
"""

title = re.search(r'^#\s+(.*)$', md)
show = re.search("(Show: )(.*)$", md)

I have tried adding re.MULTILINE but it then returns everything after the #. Can't work out what other flags, if any, I am missing here. It's not only the first re.search that doesn't work, but any that I try so likely some other flag?

Carl
  • 1,346
  • 15
  • 35
  • 2
    In your regex101 examples, you have the multiline flag enabled, but you don't in your python code. `title = re.search(r'^#([\s]+)+(.*)$', md, re.M)` works fine (note too busy writing comment to downvote) – Nick Aug 21 '23 at 08:16
  • 2
    Also, `([\s]+)+` should be re-written as `\s+` or you may run into a catastrophic backtracking. And you may safely remove `$` in both expressions. – Wiktor Stribiżew Aug 21 '23 at 08:17
  • Thanks but I tried the multiline flag in python and it just returns everything after the #, and without it it returns `None` – Carl Aug 21 '23 at 08:23
  • Try replacing `.*` with `[^\n]*+` – mousetail Aug 21 '23 at 08:30
  • Thanks @mousetail, but unfortunately that doesn't do the trick either. None of these regexs seem to work in my python script as they do in regex101 – Carl Aug 21 '23 at 08:43

0 Answers0