-3

I have a string that looks like this

![video](Title of the video)[URL of the video].

How can I get the title of the video and the URL of the video in one RegEx match without any other part of the string?

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
NduJay
  • 500
  • 1
  • 6
  • 13

1 Answers1

0

Assign capturing groups and use match_obj.groups() to return them as a tuple.

# s is the input string
title, url = re.match(r"!\[.*\]\((.*)\)\[(.*)\]", s).groups()

For the sake of readability, I prefer writing it using re.VERBOSE over a one-liner:

patt = re.compile(r"""
    !
    \[ .* \]    # video
    \( (.*) \)  # group
    \[ (.*) \]  # url
    """, re.VERBOSE)

title, url = patt.match(s).groups()
Bill Huang
  • 4,161
  • 1
  • 11
  • 28