Home > OS >  How to create a regular expression to replace a url?
How to create a regular expression to replace a url?

Time:01-19

I'm trying to create a regular expression using re.sub() that can replace a URL from a string for example.

tool(id='merge_tool', server='http://localhost:8080')

I created a regular expression that returns a string something like given below.

 a = "https:192.168.1.1:8080"

 re.sub(r'http\S ', a, "tool(id='merge_tool', server='http://localhost:8080')")

results:

 "tool(id='merge_tool', server='https:192.168.1.1"

Or if I provide this URL:

 b = 'https:facebook.com'

 re.sub(r'http\S ', b, "tool(id='merge_tool', server='http://localhost:8080')")

Results:

 "tool(id='merge_tool', server='https:facebook.com"

How to fix this so that it can return the entire string after replacing the URL?

CodePudding user response:

You can use

re.sub(r"http[^\s'] ", b.replace('\\', '\\\\'), "tool(id='merge_tool', server='http://localhost:8080')")

Note that

  • http[^\s'] will match http and then any one or more chars other than whitespace and single quote
  • b.replace('\\', '\\\\') is a must for cases where replacement literal string is dynamic, and all backslashes in it must be doubled to work as expected.
  • Related