Skip to content
Advertisement

How to append something to the beginning of Regex matches?

This is the regex code:

JavaScript

It returns me the output of each URL which doesn’t have the https header in front. For example:

JavaScript

For this, I want to append “https://example.com” in the beginning. I don’t want a for loop, is there any efficient way of doing it using re.sub?

Advertisement

Answer

You may use this regex in re.sub:

JavaScript

RegEx Demo

Code:

JavaScript

RegEx Details:

  • (?<!:/): Negative lookbehind to assert that we don’t have :/ at previous position
  • (/sports/[a-z0-9/.:-]*[0-9.]+cms): Match your text and capture in group #1
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement