Add-On REGEX Flashcards

1
Q

Show an example of the following anchors?

  • Beginning of a string
  • End of a string
  • Word boundary
A

‘^SearchString’
‘SearchString$’
‘\bSearchString\b’

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

What are character classes for:
any lowercase letter
any uppercase letter
any letter
hexadecimal values

A

[[:lower:]]
[[:upper:]]
[[:alpha:]]
[[:alnum:]]

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

Regex example to check if only one char was entered?

A

’^[[:alpha:]]$’

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

Write a pattern that matches any word that begins with b, followed by a vowel, and ending with t.

A

\bb[aeiou]t\b

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

Match any digit or carat symbol

Match line that does not contain numbers

Match any line containing a digit

Match any line containing a minus, zero or nine

A

[0-9^]

[^0-9]

[0-9]

[-09]

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

What are the special character classes for:
[0-9] (digits)
[\t\r\n] (whitespaces)
[A-Za-z0-9_]

A

[[:digit:]]
[[:space:]]
[[:alnum:]]

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

Regex should check only one of the following words item0, item1….

A

^item[[:digit:]]$

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

Find any three character?

A

‘\b[[:alnum:]]\b’

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

Find any line that contains only one character?

A

’^[[:alnum:]]$’

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

Match any word that contains three lowercase letters?

A

‘\b[[:lower:]]{3}\b’

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

5 ‘a’ characters
5 or more ‘a’ characters
between 5 and 7 ‘a’ characters
between 3 and 5 lowercase characters

A

a{5}
a{5,}
a{5,7}
[[:lower:]]{3,5}

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

What is the shorthand version of:
a{0,} - zero ore more
a{1,} - one or more
a{0,1} - zero or one

A

a*
a+
a?

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

Find any number, whether it’s a whole number or a decimal

A

’[[:digit:]]+.?[[:digit:]]*’

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

Search for dog or cat or fish

A

dog|cat|fish

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

Search for cat or bat?

A

cat|bat

[cb]at

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

Search for:
hack, crack, hacking, hacked, cracking, cracked

A

(hack|crack)(ing|ed)?

17
Q

egrep command to search for IP address?

A

egrep ‘^([[:digit:]]{1,3}.){3}[[:digit:]]{1,3}$’ testgr.txt

18
Q

egrep command that matches either 202-224-1228 or (202)224-1228?

A

egrep ‘(([[:digit:]]{3}))|([[:digit]]{3}-)[[:digit:]]{3}-[[:digit]]{4}’ testgr.txt

19
Q

egrep command that matches either “color” or “colour”?

A

egrep ‘colo[u]?r’ testgr.txt

20
Q

How can I do the following:

  1. Reverse grep
  2. Case insensitive
  3. show a byte offset in the input file to the match
  4. show only the match
  5. Color the exact match
A
  1. grep -v
  2. grep -i
  3. grep -b
  4. grep -o
  5. grep –color