-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
  • 607,720
  • 39
  • 448
  • 563
NduJay
  • 760
  • 1
  • 10
  • 23

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,491
  • 2
  • 13
  • 31