Regex to match a url/link with line breaks

Question:

I want match url from references.But some url has line brakes in it.

Example text = Yale Project on Climate Change Communication. New Haven, CT: xxx University and George
Mason University; 2015. p. 1–62. Available from: https://example.xxx.edu/wp-content/
uploads/2015/04/Global-Warming-CCAM-March-2015.pdf.

want to match: https://example.xxx.edu/wp-content/uploads/2015/04/Global-Warming-CCAM-March-2015.pdf

Asked By: Siam Sadman

||

Answers:

Try (Regex demo.)

txt = """
Yale Project on Climate Change Communication. New Haven, CT: xxx University and George
Mason University; 2015. p. 1–62. Available from: https://example.xxx.edu/wp-content/
uploads/2015/04/Global-Warming-CCAM-March-2015.pdf. This is another text just for example"""


import re

pat = re.compile(r"https?://[Sn]+")

for url in pat.findall(txt):
    print(url.replace("n", "").strip("."))

Prints:

https://example.xxx.edu/wp-content/uploads/2015/04/Global-Warming-CCAM-March-2015.pdf
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.