Regex Flashcards
https://regexone.com/
How do you indicate any digit?
\d
what characters are typcially part of regex?
All ASCII characters - https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
Does the default pattern match from the start or any postion in the string?
Any position. You need to indicate if you want it to start at the beginning etc.
What is the wildcard character?
. The . matches a single character.
how do you match a . character?
. because . is a metacharacter.
How do you match only a specific set of characters?
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 do you exclude only a specific set of characters?
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 do you match characters in a list of sequential characters?
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 are multiple character ranges tested?
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 about the number of repetitions of characters that we want to match? How do you combine it with ranges?
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)
match wazzzzzup
match wazzzup
skip wazup
wa[z]{2,5}
What is Kleene Star and the Kleene Plus?
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)
Using Kleen Start
match aaaabcc
match aabbbbc
match aacc
skip a
a+b*c+
How does optionality work?
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.
Using optionality:
match 1 file found?
match 2 files found?
match 24 files found?
skip No files found.
\d+ files? found\?