From bf21c1ca0521f8560760d04b68e1e4b331256c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 21 Nov 2018 14:24:32 +0100 Subject: [PATCH] Regex --- README.md | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1a0d086..b242bd8 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ index = .index() # Returns first index of item. Dictionary ---------- ```python +.keys() +.values() .items() .get(key, default) .setdefault(key, default) @@ -162,6 +164,7 @@ type() # / / ... ```python import numbers isinstance(, numbers.Number) # Integral, Real, Rational, Complex +callable() # Is element a function ``` @@ -175,6 +178,7 @@ String = .startswith() # Pass tuple of strings for multiple options. = .endswith() # Pass tuple of strings for multiple options. = .isnumeric() # True if str contains only numeric characters. + = .index() # Returns first index of substring. ``` ### Print @@ -186,14 +190,21 @@ print( [, , end='', sep='', file=]) # Use 'file=sys.stderr' for e ### Regex ```python import re -re.sub(, new, text, count=0) # Substitutes all occurrences. -re.search(, text) # Searches for first occurrence of pattern. -re.match(, text) # Searches only at the beginning of the string. -re.findall(, text) -re.split(, text, maxsplit=0) # Use brackets in regex to keep the matches. ``` -* **'Search' and 'match' functions return a 'Match' object. Use '.group()' method on it to get the whole match, or '.group(1)' to get the part in first bracket.** +```python + = re.sub(, new, text, count=0) # Substitutes all occurrences. + = re.findall(, text) + = re.split(, text, maxsplit=0) # Use brackets in regex to keep the matches. +``` + +#### Functions that return Match object. Use '.group()' method on it to get the whole match, or '.group(1)' to get the part in first bracket: +```python + = re.search(, text) # Searches for first occurrence of pattern. + = re.match(, text) # Searches only at the beginning of the string. + = re.finditer(, text) # Searches for all occurences of pattern. +``` + * **Parameter 'flags=re.IGNORECASE' can be used with all functions. Parameter 'flags=re.DOTALL' makes dot also accept newline.** * **Use '\\\\1' or r'\1' for backreference.** * **Use ? to make operators non-greedy.** @@ -391,10 +402,11 @@ any(el[1] for el in ) ### Namedtuple, Enum, Class ```python from collections import namedtuple -Point = namedtuple('Point', list('xy')) +Point = namedtuple('Point', 'x y') from enum import Enum -Direction = Enum('Direction', list('nesw')) +Direction = Enum('Direction', 'n e s w') +Cutlery = Enum('Cutlery', {'knife': 1, 'fork': 2, 'spoon': 3}) Creature = type('Creature', (), {'position': Point(0, 0), 'direction': Direction.n}) ``` @@ -486,7 +498,7 @@ class (Enum): ``` ```python -Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon']) +Cutlery = Enum('Cutlery', 'knife', 'fork', 'spoon']) Cutlery = Enum('Cutlery', 'knife fork spoon') Cutlery = Enum('Cutlery', {'knife': 1, 'fork': 2, 'spoon': 3}) list() # == [, , ...]