Regular Expressions Flashcards

1
Q

What is a regular expression?

A

It is a language for describing strings of symbols

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

How do you match characters in RegExps?

A

We use a ‘/’ at the beginning and end of the sequence of characters we want to match

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

What do square brackets indicate in RegExps?

A

They indicate a set of possible single-character matches (e.g. [Ww]oodchuck means match Woodchuck or woodchuck)

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

What does a dash inside a bracket imply in RegExps?

A

It means a character range - e.g. [A-Z] mean match any upper case letters

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

What does a caret (^) mean when it is the first character in RegExps?

A

It means the complement of the set of characters in the range

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

What are named ranges in RegExps?

A

They are some ranged patterns using backslash notation for common patterns (e.g. \d means any digit, \D means any non-digit, \w means any alphanumeric/underscore)

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

What does a wildcard, ‘.’, mean in RegExps?

A

It means match any character

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

How can matches be forced to be at the beginning or end of a word or line?

A

^ means start of line
$ means end of line
\b means word boundary
\B means non-word boundary

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

What does a question mark mean in RegExps?

A

It means that a character is optional

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

What does the Kleene star operator mean (*)?

A

It means match zero or more occurrences of the previous expression

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

What does Kleene + mean in RegExps?

A

It means match 1 or more occurrences of a character

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

What do braces {n} or {n, m} mean in RegExps?

A

{n} - It means to match n occurrences of the expression

{n, m} - It means to match a n to m occurrences of the expression

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

What must be considered about the * and + operators?

A

They are greedy, meaning that they match as much text as possible

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

What can we do to make the * and + operators non-greedy?

A

Add a question mark to the end of the operator, so use *? or +? instead

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

What does the pipe ‘|’ operator do?

A

It means to match this | or this

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

What does priority does the pipe operator have?

A

It has parsing priority lower than the sequence

17
Q

How do we escape a special meaning?

A

We add a backslash in order to get the actual meaning of a special character

18
Q

How do we capture groups?

A

We can use parentheses for grouping, which will match whatever is in the parentheses. It is also assigned in a register that we can then refer to later by using \ns\, where n is a number depending on the number of parentheses used

19
Q

How can we forbid a groups capture?

A

We can use the non-capturing group indicator ?: after opening the parentheses

20
Q

What are lookaround assertions?

A

It is a pattern that successfully matches something but does not consume the matched text

21
Q

What forms of lookaround are there?

A

Lookahead and Lookbehind, and we can have a negative form for both of these

22
Q

What are some limitations of Pattern Matching?

A

We get false positives, strings incorrectly matches

We get false negatives, strings that we incorrectly missed

23
Q

What should be remembered with pattern matching?

A

Law of Diminishing Returns, 80:20 rule, Pareto Principle

24
Q

When should RegExps not be used?

A
When they are too long
When there are parsers
When there are better tools to do the job
When parsing human writing
When validating some types of data
When your code will be read many times