Title: Theory / Unimportant / CS / Programming / Python / Lib / re 06.2
Tags: FromInstruction computer_to_instruction hasNotNewEffectReturnsMeaning some_instruction comment declaration
On every note of this deck, unless a note says otherwise:
Language: Python
Chapter: 6

Question: How to apply a repetition qualifiers to another one ?
Answer: Parenthesis the first one
Chapter: 6
Section: 2

Question: Note
Answer: The only content here is the non-standard one. For standard re; see regular expression anki package
Chapter: 6
Section: 2

Question: Property of the regex A|B.
Answer: If A match, B is not going to be tested, even if it would produce a longer chain
Chapter: 6
Section: 2
Context: Python

Instruction: re.DOTALL
Effect: Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.
Section: 2.2
Abbreviation: re.S

Instruction: re.VERBOSE
Effect: This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pattern is ignored, except when in a character class or when preceded by an unescaped backslash. When a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.
Section: 2.2
Abbreviation: re.X

Instruction: re.search(pattern,string,flags=0)
Section: 2.2
Returns: Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

Instruction: re.match(pattern,string,flags=0)
Section: 2.2
Returns: If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.
Note that even in MULTILINE mode, it will only match at the beginning of the string and not at the beginning of each line.

Instruction: re.fullmatch(pattern,string,flags=0)
Section: 2.2
Returns: a corresponding match object if the whole string matches the regular expression pattern
one if the string does not match the pattern; note that this is different from a zero-length match.

Instruction: re.split(pattern,string,maxsplit=0,flags=0)
Effect: Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list.
Section: 2.2

Instruction: re.findall(pattern,string,flags=0)
Section: 2.2
Returns: All non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

Instruction: re.finditer(pattern,string,flags=0)
Section: 2.2
Returns: An iterator yielding match objects over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result unless they touch the beginning of another match.

Instruction: re.sub(
pattern,
repl,
string,
count=0,
flags=0
)
Effect: If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string.
Section: 2.2
Returns: The string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is, \n is converted to a single newline character, \r is converted to a carriage return, and so forth. Unknown escapes such as \& are left alone. Backreferences, such as \6, are replaced with the substring matched by group 6 in the pattern.
The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced. Empty matches for the pattern are replaced only when not adjacent to a previous match.

Instruction: re.subn(pattern,repl,string,count=0,flags=0)
Section: 2.2
Returns: a tuple (new_string, number_of_subs_made), same operation as sub()

Instruction: re.escape(pattern)
Effect: Escape all the characters in pattern except ASCII letters, numbers and '_'. This is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
Section: 2.2

Instruction: re.purge()
Effect: Clear the regular expression cache.
Section: 2.2

Instruction: re.error(msg,pattern=None,pos=None)
Effect: Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern. The error instance has the following additional attributes:
msg
The unformatted error message.
pattern
The regular expression pattern.
pos
The index in pattern where compilation failed (may be None).
lineno
The line corresponding to pos (may be None).
colno
The column corresponding to pos (may be None).
Section: 2.2

Instruction: (?foo) starting the regexp
Effect: Match the empty string, switch the configuration of the regexp matcher
Section: 2
Variables: foo a subset of aiLmsux
Where to use it: python's regexp

Instruction: (?:foo)
Effect: Matches the same thing than foo; does not create a group
Section: 2
Variables: foo: a regexp
Where to use it: python's regexp

Instruction: (?buz-foo:bar)
Effect: Same as bar; non grouping, where parameters of buz are added and parameters of foo are removed
Context: regexp
Section: 2
Variables: bar: a regexp,
foo, buz subsets of "imsx"

Instruction: (?P<buz>bar)
Meaning: the captured group also get the name buz
Language: Python
Chapter: 6
Section: 2
Identifier: buz
Similar to: (bar)
Where to use it: regexp

Instruction: (P=name)
Effect: Matching the text matched the first time by P in the same pattern
Context: regexp
Section: 2
Variables: name:A name introduced using (?P
Where to use it: In the regexp

Instruction: m.group(name) or m.end(name)
Effect: The text matched the first time by P in the same pattern
Context: regexp
Section: 2
Variables: name:A name introduced using (?P
m a match object created by this pattern

Instruction: \g<name>
Effect: The text matched by P in or the number of a group
Context: regexp
Section: 2
Variables: name:A name introduced using (?P
m a match object created by this pattern
Where to use it: repl or re.sub()

Language: Python
Instruction: (?#foo)
Other fields (not shown on cards):
Chapter: 6
Context: regexp
Section: 2

Instruction: (?=foo)
Effect: Matches foo but does not consume it
Context: regexp
Section: 2
Name: Lookahead assertion

Instruction: (?!foo)
Effect: Does not consume anything. Matches only if foo does not match
Context: regexp
Section: 2

Instruction: (?<=foo)
Effect: Matches if the current position in the string is preceded by a match for foo that ends at the current position.
Context: regexp
Section: 2
Assuming: The length of every words matched by foo are equal
Name: Positive lookbehind assertion

Instruction: (?(foo)bar|buz)
Effect: Will try to match with bar if the group foo(A number or intrduced by (?P ) exists, and with buz if it doesn’t. buz is optional and can be omitted.
Context: regexp
Section: 2

Instruction: re.compile(pattern,flags=0)
Effect: Transform pattern and flags into a regexp object
Section: 2.2

Instruction: re.ASCII
Effect: Flag stating that \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead of full Unicode matching.
Section: 2.2
Abbreviation: re.A

Instruction: re.DEBUG
Effect: Flag stating to display debug information
Section: 2.2

Instruction: re.IGNORECASE
Effect: Perform case-insensitive matching;
Section: 2.2
Abbreviation: re.I

Instruction: re.LOCALE
Effect: Make \w, \W, \b, \B, \s and \S dependent on the current locale. The use of this flag is discouraged as the locale mechanism is very unreliable, and it only handles one “culture” at a time anyway;
you should use Unicode matching instead, which is the default in Python 3 for Unicode (str) patterns. This flag can be used only with bytes patterns.
Section: 2.2
Abbreviation: re.L

Instruction: re.MULTILINE
Effect: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline).
Section: 2.2
Abbreviation: re.M
Extra: By default, '^'
matches only at the beginning of the string, and '$' only at the end of the
string and immediately before the newline (if any) at the end of the string.