Title: Regex Patterns Every Developer Should Know — LearnCoachAssist

Q: What does the regex metacharacter . match by default?
A: A single character of any kind except the newline character (\n). To make it also match newlines, use the s (dotall / single-line) flag, e.g. /foo.bar/s in JavaScript or re.DOTALL in Python.

Q: Which character class matches any single digit in most regex flavors?
A: [0-9] or the shorthand \d. \d is equivalent to [0-9] in ASCII; with Unicode (u flag in JS, default in Python 3 str) it matches any Unicode decimal digit, e.g. Arabic-Indic digits.

Q: What does the shorthand \w match?
A: Any word character. In ASCII mode this is [A-Za-z0-9_]; in Unicode mode it also matches letters and digits from other scripts, and the underscore.

Q: What does the shorthand \s match?
A: Any whitespace character: space, tab (\t), newline (\n), carriage return (\r), form feed (\f), and vertical tab (\v). In Unicode mode it can also match rare whitespace like the non-breaking space U+00A0.

Q: How do you match a literal dot in a regex?
A: Escape it with a backslash: \.. Inside a character class the dot is literal even without escaping, but \. still works and is the safe habit.

Q: What is the difference between + and * quantifiers?
A: * matches zero or more occurrences, so the pattern is optional and may match the empty string. + matches one or more, so at least one occurrence is required.

Q: What does the ? quantifier mean when applied to a token?
A: It makes the token optional (zero or one occurrence). For example, colou?r matches both color and colour.

Q: What is the meaning of the lazy quantifier .*??
A: It matches as few characters as possible, expanding only as needed to satisfy the rest of the pattern. Useful for extracting content between the first and last delimiter, e.g. <.*?> matches a single tag.

Q: What does the quantifier {3} mean when applied to a token?
A: Match the preceding token exactly three times. {3,} means three or more, and {3,5} means between three and five times inclusive.

Q: How do greedy quantifiers behave by default?
A: They consume as many characters as possible while still allowing the overall pattern to succeed. They then backtrack if the remainder of the pattern fails.

Q: What does the anchor ^ match?
A: The position at the start of the string. Inside a character class ([^abc]) it negates the class. With the m (multiline) flag ^ also matches after each newline.

Q: What does the anchor $ match?
A: The position at the end of the string. With the m flag it also matches before each newline. In JavaScript, without the m flag, $ still matches just before a final newline in some engines.

Q: What is a word boundary \b?
A: A zero-width assertion between a word character (\w) and a non-word character (or the start/end of the string). It does not consume any characters, e.g. \bcat\b matches cat but not concatenate.

Q: What does the negative lookahead (?!...) do?
A: It asserts that the following text does NOT match the subpattern. For example, foo(?!bar) matches foo only when it is not followed by bar.

Q: What does the positive lookahead (?=...) do?
A: It asserts that the following text matches the subpattern without consuming it. For example, \d+(?=px) matches the digits in 42px but not in 42em.

Q: What is a lookbehind assertion and how is it written?
A: It is a zero-width assertion that checks what precedes the current position. Positive: (?<=...), negative: (?<!...). JavaScript supports lookbehinds since ES2018, and Python/PCRE support them with a fixed width in older versions and variable width in PCRE2 and Python 3.11+.

Q: How do you match either cat or dog in one alternation?
A: Use the pipe: cat|dog. To group larger alternatives, wrap them in a non-capturing group: (?:cat|dog)s?.

Q: What is a capturing group in regex?
A: A parenthesized subexpression that records the matched text so it can be referenced later. By default groups are numbered left-to-right starting at 1, e.g. (\d{4})-(\d{2}) captures year and month separately.

Q: How do you reference a captured group inside the same pattern?
A: Use a backreference. In most engines \1 refers to the first capturing group, e.g. ([abc])\1 matches aa, bb, or cc. In replacement strings use $1 (Perl/JavaScript) or \1 (sed).

Q: What is a non-capturing group and when is it useful?
A: A group written (?:...) that groups tokens for quantifiers or alternation but does not record the match. Use it to keep backreferences clean and slightly improve performance, e.g. (?:ab)+.

Q: What is a named capturing group?
A: A group that can be referenced by name, written (?<name>...) in most flavors (or (?P<name>...) in Python). Access via \k<name> in the pattern and $name or group("name") in code.

Q: How do you write a character class that matches any vowel?
A: [aeiou]. Add i flag for case-insensitivity, or write [aeiouAEIOU]. Negate with [^aeiou] to match any non-vowel.

Q: How do you specify a range of characters in a character class?
A: Use a hyphen between the endpoints, e.g. [a-z] matches lowercase ASCII letters and [0-9A-F] matches hexadecimal digits. The hyphen is literal at the start, end, or after \ inside the class.

Q: What is a POSIX character class and how is it written?
A: POSIX defines named classes enclosed in [: and :] inside a bracket expression, e.g. [[:alpha:]]+ matches one or more letters. Other examples are [:digit:], [:space:], [:alnum:], [:upper:], [:lower:], [:punct:], [:xdigit:].

Q: What is the difference between greedy and possessive quantifiers?
A: Greedy quantifiers backtrack to find a match. Possessive quantifiers (written with a trailing +, e.g. .*+ in PCRE/Java) never backtrack, so they can fail faster and prevent catastrophic backtracking. Python 3.11 added possessive quantifiers in the regex module and Python 3.11+ in the re module via inline flags.

Q: What is atomic grouping?
A: A group written (?>...) (in PCRE/Java/Python 3.11+ re) that, once it has matched, never releases characters even if the rest of the pattern forces backtracking. It is an alternative to possessive quantifiers and helps prevent exponential backtracking.

Q: What does the i flag do?
A: Makes the match case-insensitive. /hello/i matches Hello, HELLO, and hElLo. In Unicode-aware engines it also handles case folding across scripts.

Q: What does the m (multiline) flag do?
A: Changes the behavior of ^ and $ so they match at the start and end of every line within the input, not just the start and end of the whole string. It does NOT change how . behaves; that is the s flag.

Q: What does the s flag (dotall / single-line) do?
A: Makes the dot . also match newline characters. In JavaScript and PCRE this is s; in Python it is the inline (?s) or re.DOTALL.

Q: What does the g flag do in JavaScript?
A: Tells the regex to find all matches in the string instead of stopping at the first. Methods like String.prototype.matchAll and String.prototype.replace with a regex need it to iterate every match.

Q: How do you write a Unicode property escape in JavaScript regex?
A: Use the \p{...} syntax with the u flag, e.g. \p{Letter} or \p{Script=Greek} for Greek letters, or \p{Emoji} with the v flag for emoji sequences.

Q: What is a Unicode property in PCRE / Python regex?
A: A way to match characters by Unicode category or script, e.g. \p{L} for any letter, \p{Nd} for decimal digits, \p{Zs} for space separators. The uppercase \P negates the property.

Q: What is a backreference used for?
A: To match the same text that was previously captured. Common uses: finding doubled words with \b(\w+)\s+\1\b, or matching HTML tags with <(\w+)>.*?</\1>.

Q: How do you match an optional s at the end of a word like https??
A: Append the ? quantifier to the literal: s?. The full pattern https? matches both http and https but not httpss.

Q: How do you match a quoted string with possibly escaped quotes inside?
A: Use a pattern like "(?:\\.|[^"\\])*". It greedily matches a double quote, then any number of either an escaped character (\.) or a non-quote, non-backslash character, then a closing quote.

Q: What is the regex pattern for a valid IPv4 address?
A: A common practical pattern is ^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$. Each octet must be 0-255 and is written out to forbid leading zeros in some variants.

Q: What is a simple regex to validate an email address?
A: A practical approximation is ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$. Note that the official RFC 5321/5322 grammar is far more permissive, and a single regex cannot reliably enforce every rule; always send a confirmation email.

Q: What is a regex pattern that matches a URL starting with http or https?
A: A common one is https?://[^\s/$.?#].[^\s]*. The first part enforces the scheme, the second part forbids whitespace and the URL delimiter characters, and the rest allows path/query/fragment characters.

Q: What does [A-Za-z]+ match?
A: One or more consecutive ASCII letters. The + requires at least one, and the character class excludes digits, underscores, and accented letters.

Q: How do you match a digit, a hyphen, and three more digits?
A: \d-\d{3}. The hyphen is literal because it is not in a character class.

Q: How do you match only whitespace-only lines?
A: ^\s+$ with the m flag, or ^[ \t]+$ if you want to exclude blank lines that contain newlines.

Q: How do you split a string on one or more whitespace characters?
A: Use \s+ as the separator, e.g. "a b\tc".split(/\s+/) returns ["a","b","c"]. Most languages' split functions discard empty matches, so leading whitespace does not produce an empty first element.

Q: What is the difference between \b and \B?
A: \b is a word boundary, the position between a \w and a \W character (or string edge). \B is the opposite: a position that is NOT a word boundary. \Bcat\B matches concatenate but not cat or catnip.

Q: What is the regex for matching an HTML tag and its contents?
A: A simplified pattern is <([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>.*?</\1>. It captures the tag name and uses a backreference to require the same name in the closing tag. Parsing real HTML with regex is generally discouraged; use a proper parser.

Q: How do you match any character that is NOT a digit?
A: \D, equivalent to [^0-9] in ASCII mode, or [^\d] using a negated class. \D+ matches one or more non-digit characters.

Q: How do you match any character that is NOT a word character?
A: \W, equivalent to [^A-Za-z0-9_] in ASCII mode. Useful for stripping punctuation, e.g. replace \W+ with a single space.

Q: How do you match any non-whitespace character?
A: \S, the negation of \s. \S+ matches one or more characters that are not space, tab, or newline.

Q: What is a common regex to match a date in the form YYYY-MM-DD?
A: ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$. It enforces 4-digit year, month 01-12, and day 01-31. Note that it does not validate months with fewer than 31 days or leap years.

Q: How do you capture the area code from a US phone number like (555) 123-4567?
A: \((\d{3})\)\s*(\d{3})-(\d{4}). The parentheses must be escaped because ( and ) are metacharacters. The three groups capture area code, prefix, and line number.

Q: How do you write a regex for a positive integer?
A: ^[1-9]\d*$ matches integers with no leading zeros. Allow zero with ^0$|^[1-9]\d*$, or use ^(0|[1-9]\d*)$.

Q: How do you write a regex for a floating-point number?
A: A practical one is ^-?\d+(\.\d+)?$. It allows an optional sign, integer part, and optional fractional part but no exponent. Add ([eE][+-]?\d+)? for scientific notation.

Q: How do you match repeated words in running text?
A: \b(\w+)\s+\1\b with case-insensitive flag if needed. The capturing group (\w+) matches a word and the backreference \1 requires the next word to be identical.

Q: What is the regex to strip HTML tags from a string?
A: A basic version is <[^>]+>, replace with empty string. It does not handle malformed HTML, CDATA sections, or comments, so prefer a real sanitizer like DOMPurify for security-sensitive contexts.

Q: How do you match lines containing the word TODO as a whole word?
A: ^.*\bTODO\b.*$ with the m flag, or simply use a regex test like /\bTODO\b/.test(line). \b ensures you do not match TODOS or MYTODO.

Q: What is the meaning of (?:) in a regex?
A: An empty non-capturing group. It has no effect on matching, but in some flavors it is used to apply flags to a subexpression, e.g. (?i:abc) turns on case-insensitivity for abc only.

Q: How do you make only part of a regex case-insensitive?
A: Use inline flags. In JavaScript-style: (?i)foo(?-i)bar matches Foo and FOO but requires lowercase bar. In Python: (?i:foo)bar or re.I scoped with (?i:...).

Q: What does the re.X (or x) verbose flag allow?
A: It lets you add whitespace and # comments inside the pattern for readability, e.g. (?x)^\d{3} # area code - \d{4}$ # line number. Whitespace in the pattern is ignored unless escaped or inside a character class.

Q: What is the difference between ^ outside and inside a character class?
A: Outside a class, ^ is the start-of-string anchor. Inside [...], when it is the first character, it negates the class, so [^a-z] matches any character that is not a lowercase ASCII letter.

Q: What does the ^|$ alternation mean in a regex?
A: It is a common idiom meaning "start OR end of string". In a multi-line context it matches the boundary of any line. Sometimes written as a character class [\^$] to match a literal caret or dollar sign, which is a frequent source of bugs.

Q: What is backtracking in regex?
A: The process by which a regex engine, after failing to match later in the pattern, returns characters previously consumed by a greedy or lazy quantifier and tries again. Necessary for correct matching but can be exponential in time on adversarial inputs.

Q: What is catastrophic backtracking?
A: A pathological case where a poorly written pattern causes the regex engine to try an exponential number of combinations before failing. Classic example: (a+)+$ on a string of many as followed by a non-a character. Atomic groups or possessive quantifiers can prevent it.

Q: What is the difference between a DFA and an NFA regex engine?
A: A DFA (deterministic finite automaton) never backtracks, runs in linear time, and cannot support backreferences or lookaround. An NFA (non-deterministic) engine, used by PCRE, Python re, JavaScript, and most modern libraries, supports all those features but may backtrack.

Q: Which regex engine does JavaScript use?
A: The V8 and SpiderMonkey engines implement ECMAScript regex, which is an NFA-based engine. Modern engines (V8 irregexp) compile patterns to specialized bytecode and have added optimizations, but semantics are still NFA-based.

Q: What is the String.prototype.replace callback signature in JavaScript?
A: str.replace(regex, (match, ...captures, offset, fullString) => replacement). The first argument is the full match, followed by the captured groups, then the offset and the original string. Use it for dynamic replacements like reordering date components.

Q: How do you replace all whitespace runs with a single space?
A: In JavaScript: str.replace(/\s+/g, ' '). In Python: re.sub(r'\s+', ' ', text). In sed: s/[[:space:]]\+/ /g.

Q: How do you capture the value of a key like name="alice" in HTML attributes?
A: name\s*=\s*"([^"]*)". The captured group holds the value. Use ['"] for either quote type, or [^'"]* to allow mixed quotes.

Q: How do you match a hexadecimal color code like #1A2B3C?
A: ^#([0-9A-Fa-f]{3}){1,2}$. It allows either 3-digit shorthand or 6-digit notation. With the i flag you can drop the A-F uppercase letters and write [0-9a-f].

Q: What does the regex /(?<=\$)\d+(\.\d{2})?/ match?
A: A positive-lookbehind that asserts the position is preceded by $, then one or more digits optionally followed by a dot and exactly two digits. It matches a price like 19.99 in $19.99 without including the dollar sign in the match.

Q: How do you write a regex that matches only digits at the start of a string?
A: ^\d+ matches one or more digits anchored at the start. To require the whole string be digits, use ^\d+$. Without the ^ it would match the leading digit run anywhere in the string.

Q: How do you match everything up to the first occurrence of a comma?
A: ^[^,]* matches from the start to (but not including) the first comma, or none at all if there is no comma. In a multi-line context it only operates on the first line.

Q: How do you match lines that contain exactly 10 characters?
A: ^.{10}$ with the m flag. ^ and $ delimit the line, and . matches each of the 10 characters (newline excluded unless s flag).

Q: What is the difference between \z and \Z in PCRE?
A: \Z matches at the end of the string, or before a final newline. \z matches strictly at the very end of the string, with no exception for a trailing newline. Use \z when you need to reject a trailing newline.

Q: What does the \K escape do in PCRE / Perl?
A: It resets the start of the reported match to the current position. Everything matched before \K is consumed but not included in the match. Useful for replacements, e.g. s/foo\Kbar/baz/ replaces only bar in foobar.

Q: How do you match a JavaScript identifier (variable name)?
A: A practical pattern is ^[A-Za-z_$][A-Za-z0-9_$]*$. It requires a letter, underscore, or dollar sign as the first character, followed by any number of word characters or dollar signs. ES2015 also allows Unicode letters, which require the u flag and \p{ID_Start} / \p{ID_Continue}.

Q: How do you match a CSS hex color including the 3-, 6-, and 8-digit forms?
A: ^#(?:[0-9a-fA-F]{3}){1,2}(?:[0-9a-fA-F]{2})?$. The optional 8-digit form is the 6-digit color plus a 2-digit alpha channel. This pattern is greedy in the right places: 3+3 = 6, and the optional pair of hex digits appends for 8.

Q: What is a regex to match a number with thousand separators like 1,234,567?
A: ^\d{1,3}(,\d{3})*$. It allows one to three digits, followed by zero or more groups of a comma and exactly three digits. It does not allow trailing commas, leading commas, or a comma after a non-three-digit group.

Q: How do you match the file extension of a filename?
A: \.([^.\\/]+)$. It captures one or more non-dot, non-slash characters at the end of the string after the last dot. Use it to extract extensions like tar from archive.tar.

Q: What is the regex pattern that matches any whitespace at the end of a line?
A: [ \t]+$ with the m flag, or simply \s+$ if you also want to match trailing newlines as part of the same line. This is useful in linters that flag trailing whitespace.

Q: How do you match a string containing only ASCII letters and digits?
A: ^[A-Za-z0-9]+$. The anchors ensure the entire string is consumed, and the character class restricts the content. Add a length check separately if you need an exact length.

Q: What does the \R escape match in PCRE / PHP?
A: Any Unicode line break sequence: \r\n, \n, \r, \v (vertical tab), \f (form feed), U+0085, U+2028, and U+2029. Useful for splitting on line breaks in cross-platform text.

Q: What is a regex to extract every number from a string?
A: In JavaScript: str.match(/-?\d+(\.\d+)?/g) with the g flag. In Python: re.findall(r'-?\d+(?:\.\d+)?', text). The pattern optionally allows a sign and decimal part.

Q: How do you write a regex to match an integer in any base from 2 to 36?
A: There is no built-in way; you must enumerate: ^[01]+$|^[0-2]+$|... is impractical. A more practical approach uses ^(?:[0-1]+|...)$ per base, or you normalize the input and convert it numerically. Use the int(s, base) constructor in Python with a separate regex to validate the digit range.

Q: How do you match a credit-card-shaped 16-digit number with optional separators?
A: ^\d{4}([ -]?)\d{4}\1\d{4}\1\d{4}$. The backreference \1 ensures that whatever separator is used (space, dash, or none) is consistent across all four groups. This is a shape check, not a Luhn-check; always run a checksum before accepting.

Q: What is the regex for a slug (lowercase letters, digits, and hyphens, not starting or ending with a hyphen)?
A: ^[a-z0-9]+(?:-[a-z0-9]+)*$. It allows letters and digits, with optional hyphen-separated segments, and disallows leading, trailing, or consecutive hyphens.

Q: How do you match a string that contains a digit somewhere?
A: .*\d.* or, in a stricter check, \d is enough because any match anywhere in the string counts. To require at least one digit, use ^(?=.*\d).*$ with a lookahead if you plan to add other rules.

Q: How do you match a string of exactly 8 lowercase hex characters (a short hash)?
A: ^[a-f0-9]{8}$. Anchors require the entire string, the class lists allowed characters, and the quantifier fixes the length.

Q: What is the difference between re.search and re.match in Python?
A: re.match anchors the pattern at the start of the string (equivalent to adding \A), returning a match only if the pattern matches the prefix. re.search scans the entire string and returns a match wherever the pattern first succeeds. Use ^ explicitly with re.search to anchor at the start.

Q: What is the equivalent of re.match in JavaScript?
A: JavaScript's String.prototype.match without the g flag behaves like a search, finding the first match anywhere. To anchor at the start, prefix the pattern with ^. There is no separate match method that anchors by default.

Q: How do you escape all regex metacharacters in a user-supplied string?
A: Use a built-in helper. JavaScript has no standard one; a common one-liner is str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'). Python: re.escape(str). Java: Pattern.quote(str). Never embed raw user input as a regex without escaping.

Q: What does the pattern ^.*$ actually do?
A: It matches a whole line in multiline mode, or the entire string otherwise. The dot-star is redundant when you are already anchoring at both ends, so ^.*$ is effectively a more expensive version of just ^$ for empty-line checks.

Q: How do you match a relative URL path like /users/42?
A: ^/[A-Za-z0-9._~!$&'()+,;=:@/-]*$. This is a simplified RFC 3986 path character class with the unreserved set plus the sub-delims and a few others commonly seen. For strict validation you should percent-decode and re-validate segments.

Q: What is the difference between \A and ^?
A: \A matches only at the absolute start of the input and is not affected by the multiline flag. ^ matches the start of the input by default and at the start of every line with the m flag. Prefer \A when you want true "start of string" semantics.

Q: What does the \Z anchor do?
A: It matches at the end of the string, or just before a final newline. In PCRE and Python it is similar to $ with the multiline flag, except \Z is not affected by the m flag, while $ is anchored at end-of-string in single-line mode.

Q: What is a regex to match a version number like 1.2.3?
A: ^\d+\.\d+\.\d+(?:-[\w.]+)?(?:\+[\w.-]+)?$. The first part matches three numeric components, the optional -... captures a pre-release identifier, and the optional +... captures build metadata, following semver 2.0.0.

Q: What is a regex to match a UUID v4?
A: ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$. It enforces the 8-4-4-4-12 grouping, the 4 in the version position, and the 8/9/a/b bits in the variant nibble, matching the v4 specification.

Q: What is a regex to extract the domain from a URL?
A: A rough pattern is https?://([^/]+)/?. The capturing group holds the host portion, excluding the path. Edge cases include URLs with userinfo (user@host), ports, and IPv6 addresses in brackets; for those you need a more careful parser.

Q: How do you match the body of an HTML <script> tag?
A: A simple pattern is <script\b[^>]*>([\s\S]*?)</script>. The lazy quantifier and the [\s\S] trick (any char including newline) avoid greedy matching of nested or multiple script blocks.

Q: How do you validate a password with at least one lowercase, one uppercase, one digit, and 8+ characters?
A: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d!@#$%^&*]{8,}$. The three positive lookaheads enforce one of each, and the consuming class with a length quantifier ensures the full string matches. Always also enforce a maximum length and check against a breached-password list.

Q: What is a regex to match a 24-hour time like 23:59?
A: ^([01]\d|2[0-3]):[0-5]\d$. Hours are constrained to 00-19 or 20-23, minutes are 00-59. Add :[0-5]\d for seconds.

Q: How do you match a US zip code, optionally with the 4-digit extension?
A: ^\d{5}(?:-\d{4})?$. The non-capturing group makes the dash and four extra digits optional. \d{5} alone matches both 5- and 9-digit forms if the extension is required separately.

Q: What does the regex (?<word>\w+)\s+(\k<word>) do?
A: Matches a doubled word where the second occurrence has the same spelling as the first. (?<word>...) is a named capturing group, and \k<word> is a backreference to it, equivalent to \1 but with a name.

Q: How do you match a comma-separated list of integers?
A: A practical pattern is ^\d+(?:,\d+)*$. It requires at least one integer, with any number of ,digit groups after it. To allow whitespace around commas, change the inner pattern to \s*,\s*.

Q: How do you remove leading zeros from a number stored in a string?
A: s.replace(/^0+(\d)/, '$1') in JavaScript, or re.sub(r'^0+(\d)', r'\1', s) in Python. The ^ anchors at the start, and the captured group preserves the first non-zero digit.

Q: What is a lookahead used for in password validation?
A: To enforce multiple independent rules in a single regex without consuming characters. For example, ^(?=.*[A-Z])(?=.*\d).{8,}$ requires at least one uppercase and one digit anywhere in an 8+ character string, while the final .{8,}$ does the actual matching.

Q: How do you write a regex that matches a string with an even number of x characters?
A: ^(?:[^x]*x[^x]*x)*[^x]*$. It repeatedly matches pairs of x surrounded by non-x characters, with optional non-x filler at the ends. This is hard to express with simple quantifiers and shows the power of group iteration.

Q: What does the re.compile function do in Python?
A: It precompiles a pattern string into a Pattern object that can be reused for multiple matches. It is more efficient when the same pattern is used many times, and it lets you set flags at compile time, e.g. re.compile(r'foo', re.IGNORECASE).

Q: How do you extract the year, month, and day from a date string like 2025-03-15?
A: (\d{4})-(\d{2})-(\d{2}). The three capturing groups return the year, month, and day as separate strings. In JavaScript, use match or exec; in Python, re.search(...).groups() returns the tuple.

Q: What is the difference between + and {1,}?
A: None. They are exactly equivalent: both match one or more of the preceding token. Use whichever reads better; + is more idiomatic, and {1,} can be useful for readability in complex patterns.

Q: How do you match an empty line?
A: ^\s*$ with the m flag. The ^ and $ delimit an empty line, and \s* allows it to be filled with whitespace (so a line of only spaces also matches).

Q: What is a regex to find duplicate consecutive characters?
A: (\w)\1 matches any two identical ASCII word characters in a row. To match three or more of the same character, use (\w)\1{2,}. Useful for detecting typos or compressing runs in a string.

Q: How do you match a string containing only whitespace?
A: ^\s+$ matches one or more whitespace characters anchored at both ends. ^\s*$ would also match the empty string, which is often not desired.

Q: What is the regex to find a hex color in CSS source code?
A: #(?:[0-9a-fA-F]{3}){1,2}\b. The \b at the end ensures the match stops at a word boundary so it does not gobble trailing characters like ; or ).

Q: How do you match the entire string yes case-insensitively?
A: ^yes$ with the i flag. Without the i flag, the pattern would only match lowercase yes. The anchors prevent partial matches in larger strings.

Q: What is a regex for a comma-separated list of 1-3 digit positive integers with optional spaces?
A: ^\s*\d{1,3}\s*(?:,\s*\d{1,3}\s*)*$. Each number is 1-3 digits, surrounded by optional whitespace, separated by commas. Adjust the class if you need to allow negative numbers or decimals.

Q: What does the (\d)\1 pattern match?
A: Any two identical digits in a row, e.g. the 11 in a11b or the 00 in 100. \1 is a backreference to the first capturing group. Use (\d)\1{2,} to find 3+ identical digits in a row.

Q: How do you match a string of length 5 starting with a vowel?
A: ^[aeiou][a-zA-Z]{4}$. Anchors fix the length, the first class picks a lowercase vowel (use i flag to allow uppercase), and the second class allows any letter for the remaining four positions.

Q: What is the meaning of the (?P<name>...) syntax in Python?
A: A named capturing group in Python's re module. Access the match by name with match.group('name') or by number. JavaScript and PCRE use (?<name>...) without the P.

Q: What does the re.sub function in Python do?
A: It replaces occurrences of a pattern in a string with a replacement, returning a new string. The replacement can be a string with backreferences like \1, or a function that receives the match and returns the replacement text.

Q: What is a conditional subpattern in PCRE?
A: Written (?(condition)yes-pattern|no-pattern), it matches yes-pattern if the condition is true and optionally no-pattern otherwise. The condition can be a backreference number, e.g. (?(1)yes|no), which tests whether group 1 has been matched.

Q: How do you match a hash sign followed by a hexadecimal value like #fff or #a1b2c3?
A: ^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$. The alternation handles 3- or 6-digit forms. Anchors prevent partial matches, and case-insensitivity is achieved by including both ranges in the class.