regex-ejemplos-practicos

Regular Expression Examples

  • 4 min

Basics

Detect Uppercase Words

Finds words consisting only of uppercase letters.

Matches: "HELLO", "WORLD"

Does not match: "hello", "World"

Detect Decimal Numbers

Finds integer or decimal numbers, with or without sign.

Matches: "3.14", "-100", "+0.5"

Does not match: "abc", "123."

Detect Dates in Format

Detects dates with / or - separators.

Matches: "23/09/2023", "15-01-2022"

Does not match: "2022-09-23"

Networking

Detect URLs

Finds HTTP/HTTPS protocols followed by a domain.

Matches: "http://www.example.com", "https://sub.domain.com/path"

Does not match: "www.example.com", "http//example"

Validate an IPv4 Address

Finds addresses with four numeric blocks between 0 and 255.

Matches: "192.168.0.1", "255.255.255.0"

Does not match: "999.999.999.999", "123.456.789.0"

Validate an IPv6 Address

Finds addresses with 8 groups of 4 hexadecimal characters.

Matches: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"

Does not match: "1234:5678:90ab", "2001:::7334"

Files

Detect Filenames with Specific Extensions:

Finds filenames with extensions like .pdf, .jpg, etc.

Matches: "document.pdf", "image.jpg"

Does not match: "documenttxt", "image.docx"

Form Validations

Detect Usernames

Allows usernames between 3 and 16 characters that include letters, numbers, and underscores.

Matches: "Luis", "Pablo_92"

Does not match: "ac", "elcastigadordelasregularexpressions"

Detect Strong Passwords

Finds passwords with at least 8 characters, including uppercase, lowercase, numbers, and symbols.

Matches: "Password1!", "A1b2C3$"

Does not match: "password", "12345678"

Detect Email Addresses

Identifies a username followed by @, a domain, and an extension.

Matches: "[email protected]", "[email protected]"

Does not match: "test@domain", "user@@domain.com"

Detect Phone Numbers

Finds an optional country prefix followed by a phone number with optional spaces or dashes.

Matches: "+1-800-555-1234", "0044 20 7946 0958"

Does not match: "123456", "phone: +44 20"

Detect a Credit Card

Matches: "4111111111111111", "5500000000000004"

Does not match: "1234567890123456", "6011000000000004"

Markdown

Finds links where the text is between brackets and the URL is between parentheses.

Matches: "[LuisLlamas](https://luisllamas.es)"

Does not match: "[LuisLlamas.es](luisllamas.es)", "(https://luisllamas.es)"

Capture Images in Markdown Format

Finds the pattern of an image with description and image URL.

Matches: "![Alt text](https://www.luisllamas.es/img/regex-icon.svg)"