Regex Flashcards
How to find a plain string?
Just use the string.
i.e. abc finds abc in a file
Note that grep finds each line that contains this.
How to include spaces in a regex search?
You need to need single or double quotation marks.
Anchoring in regex
Use ^ to match for the beginning.
i.e.
^abc finds strings that start with abc
Use $ to match for the end
i.e.
abc$ finds strings that end with abc
Note that these can be combined.
i.e.
^abc$ finds lines that only contain abc
How to regex for empty lines?
^$
How to match a single character?
Use .
For example:
m..t
could find malt, meet, moot…
What is a bracket expression?
Allow matching of characters by enclosing them in []
For example:
acce[np]t would fines lines containing accent and accept
How to do a bracket expression that excludes certain characters?
Use ^ at the beginning of the expression.
i.e.
co[^l]a
would find coca, cobalt, but not cola.
How to specify a range of characters?
Inside of the brackets, use a - to connect the beginning and end.
i.e.
[1-9] is equivalent to [123456789]
[a-e] is equivalent to [abcde]
How to find some type/group of characters?
Use the predefined classes.
I.e.
[:alnum:]
Alphanumeric class
:alnum:
Alphabetic class
:alpha:
Spaces and tabs class
:blank:
Digit class
:digit:
Lowercase letters class
:lower:
Uppercase letters class
:upper: