|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type RegularExpressions struct { |
| 10 | + email *regexp.Regexp |
| 11 | + phoneNumber *regexp.Regexp |
| 12 | + url *regexp.Regexp |
| 13 | +} |
| 14 | + |
| 15 | +func main() { |
| 16 | + /* |
| 17 | + Regular expressions... |
| 18 | + */ |
| 19 | + |
| 20 | + fmt.Println("Regular expressions...") |
| 21 | + |
| 22 | + const text string = "¡Hola Mundo! Hoy es 15/04/2024. Quedan 263 días para terminar el año 2024." |
| 23 | + |
| 24 | + var regularExpression *regexp.Regexp = regexp.MustCompile(`[0-9]`) |
| 25 | + var numbers string = strings.Join(regularExpression.FindAllString(text, -1), "") |
| 26 | + |
| 27 | + fmt.Printf("\n`text` = '%s'\n", text) |
| 28 | + fmt.Printf("\nNumbers inside `text` --> %s\n", numbers) |
| 29 | + |
| 30 | + fmt.Println("\n# ---------------------------------------------------------------------------------- #") |
| 31 | + |
| 32 | + /* |
| 33 | + Additional challenge... |
| 34 | + */ |
| 35 | + |
| 36 | + fmt.Println("\nAdditional challenge...") |
| 37 | + |
| 38 | + var regularExpressions RegularExpressions = RegularExpressions{ |
| 39 | + email: regexp.MustCompile(`^[a-zA-Z0-9]*@[a-zA-Z0-9]*\.[a-zA-Z]{2,3}$`), |
| 40 | + phoneNumber: regexp.MustCompile(`^\+[0-9]{1,4} [0-9]{4} [0-9]{4}$`), |
| 41 | + url: regexp.MustCompile(`^https?://([a-zA-Z0-9]*\.)?[a-zA-Z0-9]*\.[a-zA-Z]{2,3}/?$`), |
| 42 | + } |
| 43 | + |
| 44 | + var emails [] string = [] string{ "[email protected]", |
| 45 | + |
| 46 | + |
| 47 | + "hozlucas28@melí.com", |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + var phoneNumbers []string = []string{"+12 3456 7890", |
| 52 | + "+1 1234 5890", |
| 53 | + "+1234 1234 5690", |
| 54 | + "+123456789", |
| 55 | + "+123456789 1234 5678", |
| 56 | + } |
| 57 | + |
| 58 | + var urls []string = []string{"https://www.example.cóm", |
| 59 | + "http://example.com", |
| 60 | + "https://subdomain.example.com", |
| 61 | + "http://www.example.c2.uk", |
| 62 | + "https://www.example.org", |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Println("\nEmails...") |
| 66 | + for _, email := range emails { |
| 67 | + var isValid bool = regularExpressions.email.MatchString(email) |
| 68 | + fmt.Printf("Is '%s' a valid email? %t\n", email, isValid) |
| 69 | + } |
| 70 | + |
| 71 | + fmt.Println("\nPhone numbers...") |
| 72 | + for _, phoneNumber := range phoneNumbers { |
| 73 | + var isValid bool = regularExpressions.phoneNumber.MatchString(phoneNumber) |
| 74 | + fmt.Printf("Is '%s' a valid phone number? %t\n", phoneNumber, isValid) |
| 75 | + } |
| 76 | + |
| 77 | + fmt.Println("\nUrls...") |
| 78 | + for _, url := range urls { |
| 79 | + var isValid bool = regularExpressions.url.MatchString(url) |
| 80 | + fmt.Printf("Is '%s' a valid url? %t\n", url, isValid) |
| 81 | + } |
| 82 | +} |
0 commit comments