Regular Expressions Flashcards

(20 cards)

1
Q

What does the . (dot) metacharacter match in regex?

A
  • Matches any single character except newline
  • Example: gr.p matches grep, grip, grab, etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do ^ and $ anchors do in regex?

A
  • ^ 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do square brackets [] work in regex?

A
  • Define a character class - matches any single character from the set
  • Example: [aeiou] matches any vowel, [0-9] matches any digit
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does [^] mean inside square brackets?

A
  • Negates the character class - matches any character NOT in the set
  • Example: [^0-9] matches any non-digit character
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What do * + and ? quantifiers mean?

A
    • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do curly braces {} work as quantifiers?

A
  • {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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does \ (backslash) do in regex?

A
  • Escapes special characters to match them literally
  • Example: . matches a literal dot, $ matches literal dollar sign
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are \d, \w, and \s shortcuts for?

A
  • \d matches any digit (equivalent to [0-9])
  • \w matches word characters (letters, digits, underscore)
  • \s matches whitespace characters (space, tab, newline)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do parentheses () work in regex?

A
  • Create groups for applying quantifiers to multiple characters
  • Example: (abc)+ matches abc, abcabc, abcabcabc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the pipe | symbol do in regex?

A
  • Acts as logical OR - matches either pattern before or after
  • Example: cat|dog matches either ‘cat’ or ‘dog’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do < and > represent in regex?

A
  • < matches beginning of a word boundary
  • > matches end of a word boundary
  • Example: <cat> matches ‘cat’ but not ‘category’ or ‘concatenate’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does .* differ from .+ in regex?

A
  • .* matches zero or more of any character (can match empty string)
  • .+ matches one or more of any character (requires at least one character)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does \b represent in regex?

A
  • Word boundary - matches position between word and non-word character
  • Example: \bcat\b matches ‘cat’ as whole word only
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you match a literal special character in regex?

A
  • Use backslash to escape: ., *, +, \?, [, ], (, )
  • Example: price$10 matches ‘price$10’ literally
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does [a-zA-Z0-9] match?

A
  • Matches any single alphanumeric character
  • Includes lowercase letters, uppercase letters, and digits
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you make regex case-insensitive in grep?

A
  • Use grep -i flag for case-insensitive matching
  • Example: grep -i ‘error’ file.txt matches Error, ERROR, error
17
Q

What does \n represent in regex?

A
  • Matches a newline character
  • Useful in multiline patterns or text processing
18
Q

How do you match the beginning and end of entire string?

A
  • Use \A for absolute beginning of string
  • Use \Z for absolute end of string
19
Q

What is a greedy vs non-greedy quantifier?

A
  • 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’
20
Q

How do you match one or more whitespace characters?

A
  • \s+ matches one or more whitespace (spaces, tabs, newlines)
  • Example: [ ]+ matches one or more spaces only