Level 101 Level 103
Level 102

[Quiz] Regular Expressions


106 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Ignore?
List
- a list of values, similar to arrays in Processing. Define them by having a name equal to the values separated by commas in brackets, or create an empty list by not including any val…
list example
x = ["x", "y", "z"]
index
str.index(sub[, start[, end]])
element
An element is an association to a position in a string using a human recognized format. The first element in the list would be element one.
list.append(<object>)
appends an item to the end of the list
list.pop(<index>)
Removes an item from the position specified by index. lf no position is specified, the item at the end of the list is removed and the method returns the item removed.
list.sort()
puts elements in order.
sorted(<list>)
Returns a sorted list without modifying the original list.
list.reverse()
reverses the sequence of list elements
list.count{<object>)
Returns the number of times object appears in the list.
Tuple example
x = ("x", "y", "z")
break
Used to exit a for loop or a while loop
continue
Terminate the smallest enclosing loop and immediately continue with the next iteration of the loop.
Classes
class methods are called on
class
Template for creating user-defined Objects
ClassName
A unique name by which the class will be known. Follows the same rules as variable naming (can contain letters, numbers,
object
contains both data (the actual string) and methods (functions built into the object)
def __init__( ) :
Constructor of the class object (called when an instance is created). The constructor can take arguments to initialize the attributes of the object. All attributes should be created by during the constructor phase to ensure good object state.
Constructor Code
Code body of the __init__() method
def __repr__():
The representation method. Returns a string of how the programmer wants the object to be displayed when print calls
Representation Code
Code body of the __repr__() method.
def userMethod( ):
A user defined function.Can take arguments to modify the behavior of the function. Follows the same naming conventions as variables.
User Method Code
Code body of the user defined method.
self
Represents the object itself. Used in a function to gain access to other methods or attributes defined in the object. It is
\d
digit, same as[0-9]
\s
whitespace character[ \t \r\f]
{0,} and Shortcut *
Zero or more of the previous token
{1,} and Shortcut +
One or more of the previous token
{0,1} and Shortcut ?
Zero or one of the previous token
(0-9){3}
Matches three digits in a row
\w{5,10}
Matches five to ten word characters in a row
\d{3,}
Matches three or more digits
\d*
Matches zero or more digits
\s+
Matches one or more whitespace characters
[a-zA-Z]?\d+
Matches zero or one letters followed by one or more digits
word boundary anchor
A _______ is used to match a word boundary position such as whitespace before or after a word character.
\b3\b
Matches 3, but not 33
\b [0-9]dog\b
Matches if string has a single digit followed by dog
dot ( . )
A _______ is used to match any single character (except <newline>).
match() method
The _______ is used to search through a string starting at its beginning. It returns a match object if successful, otherwise None is returned.
match() method example
matchObject = re.match( r"[a-z]+$", data, re.I )
search() method
The _______ works nearly the same as match(), however it searches the entire string, not just starting at the beginning. It returns None if no match is found.
search() method example
matchobject = re.search( r"(\d{3}-\d{3}-\d{4})", data )
findall() method
The _______ returns a list of all non-overlapping values in a specified string. This is useful when there may be more than one instance of data to extract.
findall() method example
matchobject = re.findall ( expression, data, re.M | re.I)
capture group
A _______ allows for a section of data within the regular expression match to be extracted.
Substitution
_______ is the process of matching data against a regular expression and replacing it with another value.
Substitution example
substitutedString = re.sub( r'[A-Za-z]', "*", info, 3 )
def mainO:
Used to take in a SSN and substitute entry to ***-**-XXXX
'.'
Matches any character except newline
'^'
Matches the start of a string
'$'
Matches the end of a string
'*'
Matches 0 or more repetitions of the preceding regex (as many as possible)
'+'
Matches 1 or more repetitions of the preceding regex
'?'
Matches 0 or 1 repetitions of the preceding regex
{m}
Specifies that exactly m copies of the previous RE should be matched. Fewer matches causes the entire RE not to match.
{m, n}
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many as possible.
{m, n}?
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible.
'\'
Either escapes special characters
[ ]
any single character of set
'|'
OR
(...)
Matches whatever regex is inside the parenthesis and indicates the start and end of a group
(?...)
Extension notation. First character after the '?' determines what the meaning and further syntax of the construct is.
(?aiLmsux)
Corresponding flags for letters:
(?:...)
A non-capturing version of regular parenthesis. Matches the regex inside the parenthesis but unable to retrieve. (USELESS LOL)
(?P<name>...)
Similar to regular parenthesis, but the substring matched by the group is accessible via the symbolic groupname 'name'. Groups are also numbered.
(?#...)
A comment, the contents of the parenthesis are simply ignored
(?=...)
Matches if ... matches next, but doesn't consume any of the string.
(?!...)
Matches if ... doesn't match next.
(?<=...)
Matches if the current position in the string is preceded by a match for ... that ends at the current position.
(?<!...)
Matches if the current position in the string is not preceded by a match for ...
\number
Matches the contents of the group of some number. Groups are numbered starting from 1.
\a
ASCII bell (BEL)
\b
ASCII backspace (BS)
\Z
Matches only at the end of a string
match = re.search(pattern, string)
what does search do in re?
what does re.match do?
returns a match object if true, or None on failure
what does r in front of pattern do?
tells re no characters within string have special powers.
re.sub(pattern, repl, string, max=0)
how do you search and replace in re?
what does a period do?
match any character except a new line.
hello?
match hell or hello. the o is optional
hello*
match hell plus zero or more o
hello+
match hell plus 1 or more o
\d{3}
match exactly 3 digits
\d{3,5}
match 3,4, or 5 digits
^Python
match Python at start of string or line only
Python$
match Python only at end of string or line only
\bPython\b
match Python at a word boundary
\ZPython
match Python at start of a string
python|pearl
match python or pearl
re.l
performs case insensitive matching
difference between match() and search()?
match() searches first occurence only. then stops looking.
findall()
finds all re matches and returns them as a list
pattern = re.compile('happy')
look for happy in 'I am so happy'.
string1 = "Hello, world."
match if string has length 5 or more characters.
string1 = "Hello World"
is there a word that ends in llo in hello world?
print '99' in 99 bottles of beer on wall.
string1 = "99 bottles of beer on the wall."
match = re.search(r'\d\d\d', 'p123g')
how to find '123' in p123g
match = re.search(r'..g', 'piiig')
how to find 2 any characters followed by a g
match = re.search(r'\w\w\w', '@@abcd!!')
how to find abc in &&abc!!
when matching 'of', what does (r'of.', file) match?
'of', 'of ', 'of-', 'off', but not 'offf'
re.findall(r'of*', file)
'o', 'of', 'off', 'offf'
re.findall(r'of+', file)
same as above except doesn't find the stand alone 'o's. must be 1 or more 'f's.
re.findall(r'of\b', file)
only prints out whole bunch of 'of's
re.findall(r'.of', file)
[' of', 'eof', 'eof', ' of', ' of', ' of', ' of', ' of', ' of', ' of', ' of', ' of', ' of', ' of', ' of', ' of', ' of', ' of…
re.findall(r'\wof', file)
['eof', 'eof', 'eof', 'eof', 'eof', 'eof', 'eof', 'eof', 'eof', 'eof', 'eof']