import re
entrada=input('Ingresa tu correo: ')
valido=r'(http|https|ftp)\:\/\/[a-z0-9\.-]+\.([a-z]{2,4})'
valid=r'(www)+\.[a-z0-9\.-]+\.([a-z]{2,4})'if re.match(valido,entrada) or re.match(valid,entrada):
print('Es una url valida')
else:
print('No es una url valida')
Validador de URLs en JavaScript y en Python:
// Validador de URLs en JavaScriptconst REGEX = /^https?:\/\/(\w+\.)+(\w+)(\/\w+)*(\/|\?(\w+=(\w|-)+&)*(\w+=(\w|-)+)|#.+)?$/gfunctionisUrl(url) { return REGEX.test(url) } console.log(isUrl('https://www.platzi.com')) // trueconsole.log(isUrl('hello friends')) // false
# Validador de URLs en Pythonimport re REGEX = r'^https?:\/\/(\w+\.)+(\w+)(\/\w+)*(\/|\?(\w+=(\w|-)+&)*(\w+=(\w|-)+)|#.+)?$'defisUrl(url):return re.match(REGEX, url) != None print(isUrl('https://www.platzi.com')) # true print(isUrl('hello friends')) # false
Resultado en js
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Identificador de URL</title></head><style> * { margin: 0; } .contenedor { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } .inputs { display: flex; flex-flow: column; align-items: center; padding: 2rem; } .section { padding: 0.25rem2rem; display: flex; align-items: center; } .sectioninput, .sectionspan{ margin-left: 12px; font-weight: bold; } .btn { border: 2px solid black; border-radius: 6px; cursor: pointer; padding: 20px; } </style><body><divclass="contenedor"><h1style="margin-bottom: 24px;">Identificador de URL</h1><divclass="inputs"><sectionclass="section"> Ingresa el string para identificar si es un url<inputid="string"placeholder="URL"/></section><sectionclass="section"id="resultado"></section></div><buttononclick="identificar()"class="btn">Identificar...</button></div><script>// La expresión regular la conseguí aquí https://code.tutsplus.com/es/tutorials/8-regular-expressions-you-should-know--net-6149 ya que no tengo muchos conocimientos de RegExpfunctionidentificar() { let string = document.getElementById('string').value let match = string.match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/) let resultado = document.getElementById('resultado') if (match === null){ resultado.style = 'color: red' resultado.innerHTML = `${string} no es un url` } else { resultado.style = 'color: green;' resultado.innerHTML = `${string} tiene forma de un url` } } </script></body></html>
En Python:
import re entrada=input('Ingresa tu correo: ') valido=r'(http|https|ftp)\:\/\/[a-z0-9\.-]+\.([a-z]{2,4})' valid=r'(www)+\.[a-z0-9\.-]+\.([a-z]{2,4})'if re.match(valido,entrada) or re.match(valid,entrada): print('Es una url valida') else: print('No es una url valida')
import re defis_url(str): identifier = r"[A-Za-z][A-Za-z0-9]*" domain = r"[A-Za-z0-9-_~]+"#url without query string url_format = "{schema}://{domain}(\\.{domain})*(:\\d+)?".format(schema=identifier, domain=domain) md_format = "^(\\[{url_format}\\]\s*)?((\({url_format}\))|({url_format}))$".format(url_format=url_format) result = re.match(md_format, str) return bool(result) print(is_url("[https://www.platzi.com](https://www.platzi.com)")) #true print(is_url("[https://www.platzi.com]https://www.platzi.com")) #true print(is_url("[https://www.platzi.com] https://www.platzi.com")) #true print(is_url("https://www.platzi.com")) #true print(is_url("hello friends")) #false