Regular Expressions Flashcards
(20 cards)
What does the . (dot) metacharacter match in regex?
- Matches any single character except newline
- Example: gr.p matches grep, grip, grab, etc.
What do ^ and $ anchors do in regex?
- ^ matches the beginning of a line
- $ matches the end of a line
- Example: ^error matches lines starting with ‘error’, log$ matches lines ending with ‘log’
How do square brackets [] work in regex?
- Define a character class - matches any single character from the set
- Example: [aeiou] matches any vowel, [0-9] matches any digit
What does [^] mean inside square brackets?
- Negates the character class - matches any character NOT in the set
- Example: [^0-9] matches any non-digit character
What do * + and ? quantifiers mean?
- means zero or more of the preceding character
- means one or more of the preceding character
- ? means zero or one of the preceding character (optional)
How do curly braces {} work as quantifiers?
- {n} matches exactly n occurrences
- {n,} matches n or more occurrences
- {n,m} matches between n and m occurrences
- Example: [0-9]{3} matches exactly 3 digits
What does \ (backslash) do in regex?
- Escapes special characters to match them literally
- Example: . matches a literal dot, $ matches literal dollar sign
What are \d, \w, and \s shortcuts for?
- \d matches any digit (equivalent to [0-9])
- \w matches word characters (letters, digits, underscore)
- \s matches whitespace characters (space, tab, newline)
How do parentheses () work in regex?
- Create groups for applying quantifiers to multiple characters
- Example: (abc)+ matches abc, abcabc, abcabcabc
What does the pipe | symbol do in regex?
- Acts as logical OR - matches either pattern before or after
- Example: cat|dog matches either ‘cat’ or ‘dog’
What do < and > represent in regex?
- < matches beginning of a word boundary
- > matches end of a word boundary
- Example: <cat> matches ‘cat’ but not ‘category’ or ‘concatenate’
How does .* differ from .+ in regex?
- .* matches zero or more of any character (can match empty string)
- .+ matches one or more of any character (requires at least one character)
What does \b represent in regex?
- Word boundary - matches position between word and non-word character
- Example: \bcat\b matches ‘cat’ as whole word only
How do you match a literal special character in regex?
- Use backslash to escape: ., *, +, \?, [, ], (, )
- Example: price$10 matches ‘price$10’ literally
What does [a-zA-Z0-9] match?
- Matches any single alphanumeric character
- Includes lowercase letters, uppercase letters, and digits
How do you make regex case-insensitive in grep?
- Use grep -i flag for case-insensitive matching
- Example: grep -i ‘error’ file.txt matches Error, ERROR, error
What does \n represent in regex?
- Matches a newline character
- Useful in multiline patterns or text processing
How do you match the beginning and end of entire string?
- Use \A for absolute beginning of string
- Use \Z for absolute end of string
What is a greedy vs non-greedy quantifier?
- Greedy quantifiers (*, +, {}) match as much as possible
- Non-greedy (*?, +?, {}?) match as little as possible\
- Example: ‘a.b’ vs ‘a.?b’ in string ‘aXbYb’ - greedy matches ‘aXbYb’, non-greedy matches ‘aXb’
How do you match one or more whitespace characters?
- \s+ matches one or more whitespace (spaces, tabs, newlines)
- Example: [ ]+ matches one or more spaces only