Regular Expressions Flashcards
What is a regular expression?
It is a language for describing strings of symbols
How do you match characters in RegExps?
We use a ‘/’ at the beginning and end of the sequence of characters we want to match
What do square brackets indicate in RegExps?
They indicate a set of possible single-character matches (e.g. [Ww]oodchuck means match Woodchuck or woodchuck)
What does a dash inside a bracket imply in RegExps?
It means a character range - e.g. [A-Z] mean match any upper case letters
What does a caret (^) mean when it is the first character in RegExps?
It means the complement of the set of characters in the range
What are named ranges in RegExps?
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)
What does a wildcard, ‘.’, mean in RegExps?
It means match any character
How can matches be forced to be at the beginning or end of a word or line?
^ means start of line
$ means end of line
\b means word boundary
\B means non-word boundary
What does a question mark mean in RegExps?
It means that a character is optional
What does the Kleene star operator mean (*)?
It means match zero or more occurrences of the previous expression
What does Kleene + mean in RegExps?
It means match 1 or more occurrences of a character
What do braces {n} or {n, m} mean in RegExps?
{n} - It means to match n occurrences of the expression
{n, m} - It means to match a n to m occurrences of the expression
What must be considered about the * and + operators?
They are greedy, meaning that they match as much text as possible
What can we do to make the * and + operators non-greedy?
Add a question mark to the end of the operator, so use *? or +? instead
What does the pipe ‘|’ operator do?
It means to match this | or this