Regex Flashcards

https://regexone.com/

1
Q

How do you indicate any digit?

A

\d

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

what characters are typcially part of regex?

A

All ASCII characters - https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters

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

Does the default pattern match from the start or any postion in the string?

A

Any position. You need to indicate if you want it to start at the beginning etc.

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

What is the wildcard character?

A

. The . matches a single character.

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

how do you match a . character?

A

. because . is a metacharacter.

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

How do you match only a specific set of characters?

A

There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else

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

How do you exclude only a specific set of characters?

A

To represent this, we use a similar expression that excludes specific characters using the square brackets and the ^ (hat). For example, the pattern [^abc] will match any single character except for the letters a, b, or c.

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

How do you match characters in a list of sequential characters?

A

When using the square bracket notation, there is a shorthand for matching a character in list of sequential characters by using the dash to indicate a character range. For example, the pattern [0-6] will only match any single digit character from zero to six, and nothing else. And likewise, [^n-p] will only match any single character except for letters n to p.

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

How are multiple character ranges tested?

A

Multiple character ranges can also be used in the same set of brackets, along with individual characters. An example of this is the alphanumeric \w metacharacter which is equivalent to the character range [A-Za-z0-9_] and often used to match characters in English text.

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

how about the number of repetitions of characters that we want to match? How do you combine it with ranges?

A

pecify how many repetitions of each character we want using the curly braces notation. For example, a{3} will match the a character exactly three times. Certain regular expression engines will even allow you to specify a range for this repetition such that a{1,3} will match the a character no more than 3 times, but no less than once for example.

This quantifier can be used with any character, or special metacharacters, for example w{3} (three w’s), [wxy]{5} (five characters, each of which can be a w, x, or y) and .{2,6} (between two and six of any character)

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

match wazzzzzup
match wazzzup
skip wazup

A

wa[z]{2,5}

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

What is Kleene Star and the Kleene Plus?

A

The ability to match an arbitrary number of characters

It represents either 0 or more or 1 or more of the character that it follows (it always follows a character or group)

We can use the pattern \d* to match any number of digits, but a tighter regular expression would be \d+ which ensures that the input string has at least one digit

a+ (one or more a’s), [abc]+ (one or more of any a, b, or c character) and .* (zero or more of any character)

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

Using Kleen Start

match aaaabcc
match aabbbbc
match aacc
skip a

A

a+b*c+

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

How does optionality work?

A

The ? (question mark) metacharacter which denotes optionality.

Allows you to match either zero or one of the preceding character or group. For example, the pattern ab?c will match either the strings “abc” or “ac” because the b is considered optional.

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

Using optionality:

match 1 file found?
match 2 files found?
match 24 files found?
skip No files found.

A

\d+ files? found\?

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

How are spaces, tabs and newlines handled?

A

space (␣), the tab (\t), the new line (\n) and the carriage return (\r)

In addition, a whitespace special character \s will match any of the specific whitespaces above and is extremely useful when dealing with raw input text.

17
Q

Using whitespace, how would you match:

match 1. abc
match 2. abc
match 3. abc
skip 4.abc

A

\d.\s+abc

18
Q

How do you match only the beginning or ending of something?

A

Start and the end of the line using the special ^ (hat) and $ (dollar sign) metacharacters. In the example above, we can use the pattern ^success to match only a line that begins with the word “success”

Note that this is different than the hat used inside a set of bracket [^…] for excluding characters, which can be confusing when reading regular expressions.