RegEx Flashcards

(34 cards)

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the caret symbol (^) signify in a regular expression?

A

It signifies the start of a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the dollar sign symbol ($) signify in a regular expression?

A

It signifies the end of a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

True or False: The dot (.) in a regular expression matches any single character except newline characters.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the asterisk (*) symbol do in a regular expression?

A

It matches zero or more occurrences of the preceding element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Fill in the blank: The plus sign (+) matches _____ occurrences of the preceding element.

A

one or more.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which escape character is used to match special characters in a regular expression?

A

The backslash (\) is used to escape special characters in regular expressions.

For example, to match a literal period (.) instead of the “any character” wildcard, you’d write \.. Similarly, \* matches a literal asterisk, \? matches a literal question mark, and so on.Backslash ().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of parentheses () in a regular expression?

A

They are used to group expressions and capture matched substrings. A part of a regular expression that is enclosed in parentheses (subexpression) counts as a single element as far as the operators following it are concerned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the question mark (?) signify in a regular expression?

A

It makes the preceding element optional, matching zero or one occurrence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

True or False: The curly braces ({}) are used to specify the exact number of occurrences of a preceding element.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the expression [abc] match?

A

It matches any single character ‘a’, ‘b’, or ‘c’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the expression [^abc] match?

A

It matches any single character except ‘a’, ‘b’, or ‘c’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Multiple choice: Which of the following is a valid way to match a digit in a regular expression?

A

\d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the expression \D match?

A

It matches any character that is not a digit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Fill in the blank: The expression \s matches any _____ character.

A

whitespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the expression \S match?

A

It matches any character that is not a whitespace.

17
Q

What does the expression \w match?

A

It matches any word character (alphanumeric plus underscore).

18
Q

What does the expression \W match?

A

It matches any character that is not a word character.

19
Q

True or False: The vertical bar (|) is used to denote alternation in regular expressions.

20
Q

What is the purpose of the ‘g’ flag in a regular expression?

A

It signifies a global search, finding all matches rather than stopping after the first.

21
Q

What does the ‘i’ flag do in a regular expression?

A

It makes the search case-insensitive.

22
Q

What is the purpose of the ‘m’ flag in a regular expression?

A

It allows the caret (^) and dollar sign ($) to match the start and end of each line in a multiline string.

23
Q

What is this a regex pattern for?
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}

A

email address

24
Q

What does the expression {n,m} specify in a regular expression?

A

It matches between n and m occurrences of the preceding element.

25
Fill in the blank: The expression (?i) at the beginning of a regex pattern makes the entire pattern _____-sensitive.
case.
26
What does the expression (?P...) do in a regular expression?
It creates a named group for capturing. pattern = r"(?P\d{3})-(?P\d{3})-(?P\d{4})" match = re.search(pattern, "555-123-4567") print(match.group("area_code")) # 555 print(match.group("exchange")) # 123 print(match.group("number")) # 4567
27
True or False: Regular expressions can be used for string substitution.
True. (using .replace())
28
exec method
Regular expressions also have an exec (execute) method that will return null if no match was found and return an object with information about the match otherwise. let match = /\d+/.exec("one two 100"); console.log(match); // → ["100"] console.log(match.index); // → 8
29
/i
let cartoonCrying = /boo+(hoo+)+/i; i = "ignore case"
30
\b
The \b marker matches word boundaries, positions that have a word character on one side, and a nonword character on the other.
31
look-ahead
(?=...) is a positive lookahead: It checks that a pattern follows but doesn’t consume it (doesn’t include it in the match). (?!...) is a negative lookahead: It checks that a pattern does NOT follow, without consuming characters. Practical use case — matching without including javascript "100px 200em 300px".match(/\d+(?=px)/g) // → ["100", "300"] You want the numbers, but only those followed by px. The px is checked but not included in the result. If you did consume it: javascript "100px 200em 300px".match(/\d+px/g) // → ["100px", "300px"] // Now px is part of the match
32
choice pattern
The pipe character (|) denotes a choice between the pattern to its left and the pattern to its right.
33
/g
When a g option (for global) is added after the regular expression, all matches in the string will be replaced, not just the first. console.log("Borobudur".replace(/[ou]/, "a")); // → Barobudur console.log("Borobudur".replace(/[ou]/g, "a")); // → Barabadar
34
greed
The repetition operators (+, *, ?, and {}) are greedy, meaning they match as much as they can and backtrack from there. If you put a question mark after them (+?, *?, ??, {}?), they become nongreedy and start by matching as little as possible, matching more only when the remaining pattern does not fit the smaller match.