Home > Back-end >  Regex to match a url/link with line breaks
Regex to match a url/link with line breaks

Time:11-08

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

CodePudding user response:

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?://[\S\n] ")

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