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
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
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
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