Skip to content

Commit 7169119

Browse files
authored
Merge pull request mouredev#2892 from ASJordi/main
#16 - Java
2 parents b62e6ac + 9b7e5da commit 7169119

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.*;
2+
import java.util.regex.Matcher;
3+
import java.util.regex.Pattern;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
var numbers = findNumbers("Hay más de -2 y menos de 10 números aquí, o quizás solo hay 1.");
8+
var email = validateEmail("[email protected]");
9+
var phone = validatePhoneNumber("123-456-7890");
10+
var url = validateUrl("https://www.google.com");
11+
}
12+
13+
/**
14+
* Utilizando tu lenguaje, explora el concepto de expresiones regulares,
15+
* creando una que sea capaz de encontrar y extraer todos los números
16+
* de un texto.
17+
*/
18+
static List<String> findNumbers(String str) {
19+
List<String> numbers = new LinkedList<>();
20+
Pattern p = Pattern.compile("-?\\d+");
21+
Matcher m = p.matcher(str);
22+
23+
while (m.find()) numbers.add(m.group());
24+
25+
return numbers;
26+
}
27+
28+
/**
29+
* Valida un correo electrónico.
30+
* @param email
31+
* @return
32+
*/
33+
static boolean validateEmail(String email) {
34+
Pattern p = Pattern.compile("^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$");
35+
Matcher m = p.matcher(email);
36+
return m.matches();
37+
}
38+
39+
/**
40+
* Valida un número de teléfono.
41+
* @param phone
42+
* @return
43+
*/
44+
static boolean validatePhoneNumber(String phone) {
45+
Pattern p = Pattern.compile("\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}");
46+
Matcher m = p.matcher(phone);
47+
return m.matches();
48+
}
49+
50+
/**
51+
* Valida una URL.
52+
* @param url
53+
* @return
54+
*/
55+
static boolean validateUrl(String url) {
56+
Pattern p = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
57+
Matcher m = p.matcher(url);
58+
return m .matches();
59+
}
60+
61+
}

0 commit comments

Comments
 (0)