Regular Expressions Flashcards

1
Q

What does the regex operator ^ signify?

A

The ^ operator signifies the start of a line or string. It is used to match a pattern only if it appears at the beginning.

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

What does the regex operator $ signify?

A

The $ operator signifies the end of a line or string. It is used to match a pattern only if it appears at the end.

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

What does the regex operator . signify?

A

The . operator matches any single character except a newline. It acts as a wildcard in regex.

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

What does the regex operator * signify?

A

The * operator matches zero or more occurrences of the preceding character or group. For example, a* matches ‘’, ‘a’, ‘aa’, etc.

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

What is the function of the regex operator *?

A

The * operator matches zero or more occurrences of the preceding character or group. It allows for repetition, including none at all.

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

What does the regex operator + signify?

A

The + operator matches one or more occurrences of the preceding character or group. For example, a+ matches ‘a’, ‘aa’, ‘aaa’, etc.

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

What does the regex operator ? signify?

A

The ? operator matches zero or one occurrence of the preceding character or group. For example, colou?r matches both ‘color’ and ‘colour’.

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

What does the regex operator [] signify?

A

Square brackets [] define a character set and match any one character within the set. For example, [abc] matches ‘a’, ‘b’, or ‘c’.

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

What does the regex operator [^ ] do?

A

The [^ ] operator defines a negated character set and matches any character not in the set. For example, [^abc] matches any character except ‘a’, ‘b’, or ‘c’.

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

What does the regex operator | signify?

A

The | operator acts as an OR condition and matches either pattern on its sides. For example, cat|dog matches ‘cat’ or ‘dog’.

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

What does the regex operator () signify?

A

Parentheses () are used for grouping patterns. They allow regex operators to act on the entire group as a unit.

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

What does the regex operator \d signify?

A

The \d operator matches any digit (0–9). It is equivalent to the character set [0-9].

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

What does the regex operator \D signify?

A

The \D operator matches any non-digit character. It is equivalent to the negated set [^0-9].

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

What does the regex operator \w signify?

A

The \w operator matches any word character (alphanumeric and underscore). It is equivalent to [a-zA-Z0-9_].

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

What does the regex operator \W signify?

A

The \W operator matches any non-word character. It is equivalent to the negated set [^a-zA-Z0-9_].

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

What does the regex operator \s signify?

A

The \s operator matches any whitespace character, such as spaces, tabs, or newlines.

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

What does the regex operator \S signify?

A

The \S operator matches any non-whitespace character.

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

What does the regex operator {n} signify?

A

The {n} operator matches exactly ‘n’ occurrences of the preceding character or group. For example, a{3} matches ‘aaa’.

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

What does the regex operator {n,} signify?

A

The {n,} operator matches ‘n’ or more occurrences of the preceding character or group. For example, a{2,} matches ‘aa’, ‘aaa’, etc.

20
Q

What does the regex operator {n,m} signify?

A

The {n,m} operator matches between ‘n’ and ‘m’ occurrences of the preceding character or group. For example, a{2,4} matches ‘aa’, ‘aaa’, or ‘aaaa’.

21
Q

What does the regex operator \ signify?

A

The \ operator escapes special characters, allowing them to be treated as literals. For example, \* matches the ‘*’ character instead of treating it as a regex operator.

22
Q

What does the regex operator \b signify?

A

The \b operator matches a word boundary, ensuring the match occurs at the start or end of a word.

23
Q

What does the regex operator \B signify?

A

The \B operator matches a non-word boundary, ensuring the match occurs within a word.

24
Q

What does the regex operator (?=…) signify?

A

The (?=…) operator is a positive lookahead, ensuring that a certain pattern follows the current position without consuming characters.

25
Q

What does the regex operator (?!…) signify?

A

The (?!…) operator is a negative lookahead, ensuring that a certain pattern does not follow the current position without consuming characters.

26
Q

What does the regex operator (?<=…) signify?

A

The (?<=…) operator is a positive lookbehind, ensuring that a certain pattern precedes the current position without consuming characters.

27
Q

What does the regex operator (?<!…) signify?

A

The (?<!…) operator is a negative lookbehind, ensuring that a certain pattern does not precede the current position without consuming characters.

28
Q

^hello

A

Matches any string that starts with ‘hello’. For example, it will match ‘hello world’ but not ‘a hello world’.

29
Q

.*

A

Matches zero or more of any character except newline. This can match an empty string, ‘abc’, or even ‘hello123’.

30
Q

\d{3}-\d{2}-\d{4}

A

Matches a Social Security number format, such as ‘123-45-6789’. It consists of three digits, a dash, two digits, another dash, and four digits.

31
Q

[A-Za-z]+

A

Matches one or more uppercase or lowercase letters. For example, it will match ‘Hello’ and ‘abc’ but not ‘123’.

32
Q

colou?r

A

Matches both ‘color’ and ‘colour’. The ‘?’ means the preceding character ‘u’ is optional.

33
Q

\bword\b

A

Matches the word ‘word’ as a whole word only. It won’t match ‘sword’ or ‘wording’ but will match ‘word’.

34
Q

\d+.?\d*

A

Matches integers or decimals. For example, it matches ‘123’, ‘45.67’, or ‘89.’.

35
Q

(cat|dog)

A

Matches either ‘cat’ or ‘dog’. The ‘|’ acts as an OR operator between the two options.

36
Q

^[a-z0-9_-]{3,16}$

A

Matches usernames 3 to 16 characters long, allowing lowercase letters, numbers, underscores, and hyphens. It enforces both the length and allowed characters.

37
Q

\w+@\w+.\w+

A

Matches simple email addresses such as ‘example@mail.com’. It requires a word, an ‘@’, another word, a ‘.’, and a final word.

38
Q

\s{2,}

A

Matches two or more whitespace characters in a row. Useful for finding excessive spaces.

39
Q

(?i)hello

A

Matches ‘hello’ in a case-insensitive way. It will match ‘HELLO’, ‘hello’, or ‘HeLLo’.

40
Q

[0-9]{4,}-[A-Z]{2}

A

Matches a pattern like ‘1234-AB’ where four or more digits are followed by a dash and two uppercase letters.

41
Q

(https?|ftp)://[^\s/$.?#].[^\s]*

A

Matches URLs that start with ‘http’, ‘https’, or ‘ftp’, such as ‘https://example.com’.

42
Q

^.{8,}$

A

Matches any string that is at least 8 characters long. The ‘.’ matches any character, and ‘{8,}’ enforces the minimum length.

43
Q

^[^@]+@[^@]+.[a-zA-Z]{2,}$

A

Matches valid email addresses by ensuring there is no ‘@’ at the start, no ‘@’ immediately before the domain, and the domain has at least two letters.

44
Q

([A-Z][a-z]*){2,}

A

Matches strings with at least two capitalized words, such as ‘JohnDoe’ or ‘JaneSmith’.

45
Q

([01]?\d|2[0-3]):[0-5]\d

A

Matches valid 24-hour time formats, such as ‘23:59’, ‘5:30’, or ‘00:00’.

46
Q

(?<=$)\d+(.\d{2})?

A

Matches monetary values preceded by a ‘$’ symbol, such as ‘$100’ or ‘$99.99’. It uses a positive lookbehind.

47
Q

^[A-Fa-f0-9]{6}$

A

Matches valid hexadecimal color codes, such as ‘FFAACC’ or ‘abcdef’.