Skip to content

Commit 327d04e

Browse files
authored
Merge pull request mouredev#3778 from monicavaquerano/main
#16 - python, #16 - javascript
2 parents 370033b + 286845a commit 327d04e

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// # 16 EXPRESIONES REGULARES
2+
// # Monica Vaquerano
3+
// # https://monicavaquerano.dev
4+
5+
/*
6+
* EJERCICIO:
7+
* Utilizando tu lenguaje, explora el concepto de expresiones regulares,
8+
* creando una que sea capaz de encontrar y extraer todos los números
9+
* de un texto.
10+
*
11+
* DIFICULTAD EXTRA (opcional):
12+
* Crea 3 expresiones regulares (a tu criterio) capaces de:
13+
* - Validar un email.
14+
* - Validar un número de teléfono.
15+
* - Validar una url.
16+
*/
17+
18+
// Regular Expresions
19+
20+
// Search Method
21+
let text = "hola, mundo!";
22+
23+
// with a String
24+
let x = text.search("mundo");
25+
console.log(x);
26+
27+
// with a Regular Expression
28+
x = text.search(/mundo/i);
29+
console.log(x);
30+
31+
x ? console.log("It's a match") : console.log("Not a match");
32+
33+
// Replace Method
34+
text = "Visit Berlin!";
35+
36+
// with a String
37+
x = text.replace("Berlin", "Stuttgart");
38+
console.log(x);
39+
40+
// with a Regular Expression
41+
x = text.replace(/Berlin/i, "Stuttgart");
42+
console.log(x);
43+
44+
// test() Method
45+
let pattern = /e/;
46+
x = pattern.test("The best things in life are free!");
47+
x ? console.log("El patrón existe en la string") : console.log("El patrón NO existe en la string")
48+
49+
// exec() Method
50+
const obj = /e/.exec("The best things in life are free!");
51+
console.log(obj)
52+
console.log(`Found ${obj[0]} in position ${obj.index} in the text: ${obj.input}`)
53+
54+
// toString() Method
55+
pattern = new RegExp("Hello World", "g");
56+
text = pattern.toString();
57+
console.log(text)
58+
59+
// match() Method
60+
const findDigits = (txt) => {
61+
let pattern = /\d+/g;
62+
let digits = txt.match(pattern);
63+
return digits;
64+
}
65+
66+
console.log(findDigits("The r4in in 5p4in"))
67+
68+
// Regular Expression Modifiers: Modifiers can be used to perform case-insensitive more global searches:
69+
// Modifier Description
70+
// i Perform case-insensitive matching
71+
// g Perform a global match (find all)
72+
// m Perform multiline matching
73+
// d Perform start and end matching (New in ES2022)
74+
75+
// Extra
76+
77+
// E-Mail
78+
const emailCheck = (email) => {
79+
let pattern = /^[\w.+-]+@[\w.+-]+\.[a-zA-Z]+$/g;
80+
let check = pattern.test(email);
81+
return check;
82+
}
83+
console.log("[email protected] es un email válido? =>", emailCheck("[email protected]")) // true
84+
console.log("[email protected] es un email válido? =>", emailCheck("[email protected]")) // true
85+
console.log("exampleexample.com es un email válido? =>", emailCheck("exampleexample.com")) // false
86+
console.log("[email protected] es un email válido? =>", emailCheck("[email protected]")) // false
87+
88+
// # Número de teléfono
89+
function telephoneCheck(str) {
90+
let regex = /^(1\s?)?(\(\d{3}\)|\d{3})([\-\s])?(\d{3})([\-\s])?(\d{4})$/;
91+
let phoneCheck = regex.test(str)
92+
return phoneCheck;
93+
}
94+
95+
console.log("555-555-5555 es un número válido? =>", telephoneCheck("555-555-5555")); // true
96+
console.log("1 (555) 555-5555 es un número válido? =>", telephoneCheck("1 (555) 555-5555")); // true
97+
console.log("5555555555 es un número válido? =>", telephoneCheck("5555555555")) // true
98+
console.log("(555)555-5555 es un número válido? =>", telephoneCheck("(555)555-5555")) // true
99+
console.log("5555555 es un número válido? =>", telephoneCheck("5555555")) // false
100+
console.log("123**&!!asdf# es un número válido? =>", telephoneCheck("123**&!!asdf#")) // false
101+
console.log("11 555-555-5555 es un número válido? =>", telephoneCheck("11 555-555-5555")) // false
102+
console.log("1 555)555-5555 es un número válido? =>", telephoneCheck("1 555)555-5555")) // false
103+
console.log("(6054756961) es un número válido? =>", telephoneCheck("(6054756961)")) // false
104+
console.log("27576227382 es un número válido? =>", telephoneCheck("27576227382")) // false
105+
console.log("(275)76227382 es un número válido? =>", telephoneCheck("(275)76227382")) //false
106+
107+
// # URL
108+
const urlCheck = (url) => {
109+
let pattern = /(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?\/[a-zA-Z0-9]{2,}|((https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?)|(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}(\.[a-zA-Z0-9]{2,})?/g;
110+
let urlCheck = pattern.test(url);
111+
return urlCheck;
112+
}
113+
114+
console.log("https://google.com/ es un url válido? =>", urlCheck("https://google.com/")) // true
115+
console.log("http://google.com es un url válido? =>", urlCheck("http://google.com")) // true
116+
console.log("https://www.google.com es un url válido? =>", urlCheck("https://www.google.com")) // true
117+
console.log("www.google.com es un url válido? =>", urlCheck("www.google.com")) // true
118+
console.log("google.com es un url válido? =>", urlCheck("google.com")) // true
119+
console.log("google.c es un url válido? =>", urlCheck("google.c")) // false
120+
console.log("goo/gle.c es un url válido? =>", urlCheck("goo/gle.c")) // false
121+
122+
// pattern = r"^(http[s]?://)?(www.)?[\w]+\.[\w]{2,}[/]?$"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# 16 EXPRESIONES REGULARES
2+
# Monica Vaquerano
3+
# https://monicavaquerano.dev
4+
5+
"""/*
6+
* EJERCICIO:
7+
* Utilizando tu lenguaje, explora el concepto de expresiones regulares,
8+
* creando una que sea capaz de encontrar y extraer todos los números
9+
* de un texto.
10+
*
11+
* DIFICULTAD EXTRA (opcional):
12+
* Crea 3 expresiones regulares (a tu criterio) capaces de:
13+
* - Validar un email.
14+
* - Validar un número de teléfono.
15+
* - Validar una url.
16+
*/"""
17+
18+
# Regular Expresions
19+
import re
20+
21+
text = "hola, mundo!"
22+
x = re.search(r"hola", text)
23+
24+
if x:
25+
print("It's a match")
26+
else:
27+
print("Not a match")
28+
29+
# Function Description
30+
# findall - Returns a list containing all matches
31+
# search - Returns a Match object if there is a match anywhere in the string
32+
# split - Returns a list where the string has been split at each match
33+
# sub - Replaces one or many matches with a string
34+
35+
36+
def findDigits(txt):
37+
pattern = "\d+"
38+
digits = re.findall(pattern, txt)
39+
return digits
40+
41+
42+
print(findDigits("The r4in in 5p4in"))
43+
44+
# Extra
45+
46+
47+
# E-Mail
48+
def emailCheck(email):
49+
pattern = r"^[\w.+-]+@[\w.+-]+\.[a-zA-Z]+$"
50+
check = bool(re.match(pattern, email))
51+
return check
52+
53+
54+
print("[email protected] es un email válido? =>", emailCheck("[email protected]"))
55+
print("[email protected] es un email válido? =>", emailCheck("[email protected]"))
56+
print("exampleexample.com es un email válido? =>", emailCheck("exampleexample.com"))
57+
print("[email protected] es un email válido? =>", emailCheck("[email protected]"))
58+
59+
60+
# Número de teléfono
61+
def telephoneCheck(number):
62+
pattern = r"^(1\s?)?(\(\d{3}\)|\d{3})([\-\s])?(\d{3})([\-\s])?(\d{4})$"
63+
check = bool(re.match(pattern, number))
64+
return check
65+
66+
67+
print("555-555-5555 es un número válido? =>", telephoneCheck("555-555-5555"))
68+
print("1 555-555-5555 es un número válido? =>", telephoneCheck("1 555-555-5555"))
69+
print("555-5555 es un número válido? =>", telephoneCheck("555-5555"))
70+
print("(555)5(55?)-5555 es un número válido? =>", telephoneCheck("(555)5(55?)-5555"))
71+
72+
73+
# URL
74+
def urlCheck(url):
75+
pattern = r"^(http[s]?://)?(www.)?[\w]+\.[\w]{2,}[/]?$"
76+
check = bool(re.match(pattern, url))
77+
return check
78+
79+
80+
print("https://google.com/ es un url válido? =>", urlCheck("https://google.com/"))
81+
print("http://google.com es un url válido? =>", urlCheck("http://google.com"))
82+
print("https://www.google.com es un url válido? =>", urlCheck("https://www.google.com"))
83+
print("www.google.com es un url válido? =>", urlCheck("www.google.com"))
84+
print("google.com es un url válido? =>", urlCheck("google.com"))
85+
print("google.c es un url válido? =>", urlCheck("google.c"))
86+
print("goo/gle.c es un url válido? =>", urlCheck("goo/gle.c"))

0 commit comments

Comments
 (0)