Regex Flashcards

1
Q

How to find a plain string?

A

Just use the string.
i.e. abc finds abc in a file

Note that grep finds each line that contains this.

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

How to include spaces in a regex search?

A

You need to need single or double quotation marks.

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

Anchoring in regex

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to regex for empty lines?

A

^$

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

How to match a single character?

A

Use .
For example:
m..t
could find malt, meet, moot…

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

What is a bracket expression?

A

Allow matching of characters by enclosing them in []
For example:
acce[np]t would fines lines containing accent and accept

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

How to do a bracket expression that excludes certain characters?

A

Use ^ at the beginning of the expression.
i.e.
co[^l]a
would find coca, cobalt, but not cola.

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

How to specify a range of characters?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to find some type/group of characters?

A

Use the predefined classes.
I.e.
[:alnum:]

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

Alphanumeric class

A

:alnum:

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

Alphabetic class

A

:alpha:

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

Spaces and tabs class

A

:blank:

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

Digit class

A

:digit:

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

Lowercase letters class

A

:lower:

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

Uppercase letters class

A

:upper:

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

What is a quantifier?

A

Allows you to specify the number of occurrences that must be present to match.
*
?
+
{n}
{n,}
{,m}
{n,m}
Note that this works on the PRECEEDING character.

17
Q

How to match 0 or more times?

A

*

18
Q

How to match 0 or 1 time?

A

?

19
Q

How to match 1 or more times?

A

+

20
Q

How to match exactly n times?

A

{n}

21
Q

How to match at least n times?

A

{n,}

22
Q

How to match at most m times?

A

{,m}

23
Q

How to match n to m times?

A

{n,m}

24
Q

How to include a regex symbol as a string so that it is not interpreted as regex?

A

Use an escape character.

25
Q

How to match different possible matches?

A

Use |
For example, ‘big|large|massive’ matches big, OR large, OR massive.

Note that using -E for extended regular expressions, \ can be omitted (don’t escape |).

26
Q

How to group things?

A

Use brackets. Note, must use \ on parenthesis when not using extended regex.
For example ‘(fear)?less’ finds lines that include less preceded by 1 or 0 fear.

27
Q

Special backslash expressions.

A

\b matches a word boundary
< matches an empty string at the beginning of a word
> matches an empty string at the end of a word
\w matches a word
\s matches a space

For example,
‘\bhello\b’ finds hello, but not if it is embedded in a larger word.

28
Q
A