Challenge your understanding of regular expressions for accurate email and URL pattern matching. This quiz covers fundamental regex concepts, helping you distinguish valid and invalid patterns for recognizing email addresses and web URLs effectively.
Which of the following regular expressions best matches a basic email address like user@example.com?
Explanation: ^S+@S+.S+$ correctly matches most simple email addresses by ensuring non-whitespace characters before and after the '@' and at least one period. ^S+@example.com$ only matches emails from 'example.com' and is too specific. ^[A-Za-z0-9]+$ would only match a basic word, not an email format. users*@s*example.com is overly lax and would incorrectly allow spaces within the address.
Consider matching a URL that may or may not start with 'http://' or 'https://', such as 'https://site.org' or 'site.org'. Which regex pattern correctly handles this?
Explanation: ^(https?://)?[w.-]+.[a-z]{2,}$ allows for the protocol part to be optional and matches typical domain patterns. ^http[s]?://[a-zA-Z0-9.]+$ makes the protocol required. www.[a-zA-Z]+.[a-z]{2,3} only matches URLs beginning with 'www.' and ignores many valid sites. ^(ftp://)?[w-]+.[com|org|net]+$ is specific to FTP and misuses character sets for TLDs.
Given the email pattern, which username would be valid according to the regex /^[A-Za-z0-9._%-]+$/?
Explanation: The username 'john.doe_1995' consists only of permitted characters: letters, digits, dots, underscores, and numbers. 'user+name!' includes a plus and exclamation mark, which are not allowed by the pattern. '-samuel' starts with a dash, which is not covered. 'alice#doe' contains the hash sign, which is absent from the given whitelist.
To match web addresses like 'blog.site.com' or 'news.portal.org', which regex best ensures the presence of at least one subdomain?
Explanation: ^(w+.)+w+.[a-z]{2,}$ forces at least one subdomain by requiring multiple dot-separated word groups before the top-level domain. ^w+.w+.[a-z]{2,}$ only matches domains with exactly two subdomains and TLD, not more. ^www.[a-z]+.[a-z]{2,3} restricts to 'www' as the subdomain. ^[a-z]+.[a-z]{2,}$ will only match domains without any subdomain at all.
Which regex correctly allows hyphens within domain names, to match URLs like 'my-site.com' as well as 'example.com'?
Explanation: ^[w-]+.[a-z]{2,}$ includes both word characters and hyphens, allowing flexible domain names like 'my-site.com'. ^[a-zA-Z_]+.[a-z]{2,}$ omits hyphens and restricts domains to letters or underscores. ^[w]+.[a-z]{2,}$ is also too restrictive as it excludes hyphens. ^my-site.com$ is hardcoded to match only one specific domain, making it inflexible.