Regex Flashcards
What import is needed to use regex?
import re
How does a basic regex in python looks like?
match = re.search(r’searchstring’,string)
Show an example of the following anchors?
- Beginning of a string
- End of a string
- Word boundary
re. search(‘^SearchString’,string)
re. search(‘SearchString$’,string)
re. search(‘\bSearchString\b’,string)
How to search for special characters?
re.search(‘\?’,string)
How to define the regex in a variable?
pattern = r'Yves' string1 = "Yves Sturzenegger" match1 = re.search(pattern, string1)
What are character classes for:
any lowercase letter
any uppercase letter
any letter
hexadecimal values
r’[a-z]’
r’[A-Z]’
r’[a-zA-Z]’
r’[a-fA-F0-9]’
Regex example to check if only one char was entered?
r’^[a-zA-Z]$’
Write a pattern that matches any word that begins with b, followed by a vowel, and ending with t.
\bb[aeiou]t\b
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
r’[0-9^]’
r’[^0-9]’
r’[0-9]’
r’[-09]
What are the special character classes for:
[0-9] (digits)
[\t\r\n] (whitespaces)
[A-Za-z0-9_]
\d
\s
\w
What are the special character classes for:
[^0-9] (digits)
[^\t\r\n] (whitespaces)
[^A-Za-z0-9_]
\D
\S
\W
Regex should check only one of the following words item0, item1….
^item\d$
Find any three character?
r’\b…\b’
Find any line that contains only one character?
r’^.$’
Match any word that contains three lowercase letters?
r’\b[a-z]{3}\b’