From f84d6b977f880572da64a66a64974ed3ac12913d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 6 Jun 2022 23:34:02 +0200 Subject: [PATCH] Regex, Format --- README.md | 54 +++++++++++++++++++++++++++--------------------------- index.html | 54 +++++++++++++++++++++++++++--------------------------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 6dd94a5..fb50f15 100644 --- a/README.md +++ b/README.md @@ -357,10 +357,10 @@ import re = re.split(, text, maxsplit=0) # Use brackets in regex to include the matches. = re.search(, text) # Searches for first occurrence of the pattern. = re.match(, text) # Searches only at the beginning of the text. - = re.finditer(, text) # Returns all occurrences as match objects. + = re.finditer(, text) # Returns all occurrences as Match objects. ``` -* **Argument 'new' can be a function that accepts a match object and returns a string.** +* **Argument 'new' can be a function that accepts a Match object and returns a string.** * **Search() and match() return None if they can't find a match.** * **Argument `'flags=re.IGNORECASE'` can be used with all functions.** * **Argument `'flags=re.MULTILINE'` makes `'^'` and `'$'` match the start/end of each line.** @@ -385,21 +385,21 @@ import re ``` * **By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless `'flags=re.ASCII'` argument is used.** -* **As shown above, it restricts special sequence matches to the first 128 characters and prevents `'\s'` from accepting `'[\x1c-\x1f]'` (the so-called separator characters).** +* **As shown above, it restricts all special sequence matches to the first 128 characters and prevents `'\s'` from accepting `'[\x1c-\x1f]'` (the so-called separator characters).** * **Use a capital letter for negation (all non-ASCII characters will be matched when used in combination with ASCII flag).** Format ------ ```python - = f'{}, {}' - = '{}, {}'.format(, ) + = f'{}, {}' # Curly braces also accept expressions. + = '{}, {}'.format(, ) # Or: '{0}, {1}'.format(, ) + = '%s, %s' % (, ) # Redundant and inferior C style formatting. ``` ### Attributes ```python ->>> from collections import namedtuple ->>> Person = namedtuple('Person', 'name height') +>>> Person = collections.namedtuple('Person', 'name height') >>> person = Person('Jean-Luc', 187) >>> f'{person.height}' '187' @@ -409,40 +409,40 @@ Format ### General Options ```python -{:<10} # ' ' -{:^10} # ' ' -{:>10} # ' ' -{:.<10} # '......' -{:0} # '' +{:<10} # ' ' +{:^10} # ' ' +{:>10} # ' ' +{:.<10} # '......' +{:0} # '' ``` * **Options can be generated dynamically: `f'{:{}[…]}'`.** * **Adding `'!r'` before the colon converts object to string by calling its [repr()](#class) method.** ### Strings ```python -{'abcde':10} # 'abcde ' -{'abcde':10.3} # 'abc ' -{'abcde':.3} # 'abc' -{'abcde'!r:10} # "'abcde' " +{'abcde':10} # 'abcde ' +{'abcde':10.3} # 'abc ' +{'abcde':.3} # 'abc' +{'abcde'!r:10} # "'abcde' " ``` ### Numbers ```python -{123456:10} # ' 123456' -{123456:10,} # ' 123,456' -{123456:10_} # ' 123_456' -{123456:+10} # ' +123456' -{123456:=+10} # '+ 123456' -{123456: } # ' 123456' -{-123456: } # '-123456' +{123456:10} # ' 123456' +{123456:10,} # ' 123,456' +{123456:10_} # ' 123_456' +{123456:+10} # ' +123456' +{123456:=+10} # '+ 123456' +{123456: } # ' 123456' +{-123456: } # '-123456' ``` ### Floats ```python -{1.23456:10.3} # ' 1.23' -{1.23456:10.3f} # ' 1.235' -{1.23456:10.3e} # ' 1.235e+00' -{1.23456:10.3%} # ' 123.456%' +{1.23456:10.3} # ' 1.23' +{1.23456:10.3f} # ' 1.235' +{1.23456:10.3e} # ' 1.235e+00' +{1.23456:10.3%} # ' 123.456%' ``` #### Comparison of presentation types: diff --git a/index.html b/index.html index 0e86d99..300fbd6 100644 --- a/index.html +++ b/index.html @@ -336,11 +336,11 @@ to_exclusive = <range>.stop <list> = re.split(<regex>, text, maxsplit=0) # Use brackets in regex to include the matches. <Match> = re.search(<regex>, text) # Searches for first occurrence of the pattern. <Match> = re.match(<regex>, text) # Searches only at the beginning of the text. -<iter> = re.finditer(<regex>, text) # Returns all occurrences as match objects. +<iter> = re.finditer(<regex>, text) # Returns all occurrences as Match objects.
    -
  • Argument 'new' can be a function that accepts a match object and returns a string.
  • +
  • Argument 'new' can be a function that accepts a Match object and returns a string.
  • Search() and match() return None if they can't find a match.
  • Argument 'flags=re.IGNORECASE' can be used with all functions.
  • Argument 'flags=re.MULTILINE' makes '^' and '$' match the start/end of each line.
  • @@ -362,15 +362,15 @@ to_exclusive = <range>.stop
    • By default, decimal characters, alphanumerics and whitespaces from all alphabets are matched unless 'flags=re.ASCII' argument is used.
    • -
    • As shown above, it restricts special sequence matches to the first 128 characters and prevents '\s' from accepting '[\x1c-\x1f]' (the so-called separator characters).
    • +
    • As shown above, it restricts all special sequence matches to the first 128 characters and prevents '\s' from accepting '[\x1c-\x1f]' (the so-called separator characters).
    • Use a capital letter for negation (all non-ASCII characters will be matched when used in combination with ASCII flag).
    -

    #Format

    <str> = f'{<el_1>}, {<el_2>}'
    -<str> = '{}, {}'.format(<el_1>, <el_2>)
    +

    #Format

    <str> = f'{<el_1>}, {<el_2>}'            # Curly braces also accept expressions.
    +<str> = '{}, {}'.format(<el_1>, <el_2>)  # Or: '{0}, {1}'.format(<el_1>, <el_2>)
    +<str> = '%s, %s' % (<el_1>, <el_2>)      # Redundant and inferior C style formatting.
     
    -

    Attributes

    >>> from collections import namedtuple
    ->>> Person = namedtuple('Person', 'name height')
    +

    Attributes

    >>> Person = collections.namedtuple('Person', 'name height')
     >>> person = Person('Jean-Luc', 187)
     >>> f'{person.height}'
     '187'
    @@ -378,36 +378,36 @@ to_exclusive   = <range>.stop
     '187'
     
    -

    General Options

    {<el>:<10}                                     # '<el>      '
    -{<el>:^10}                                     # '   <el>   '
    -{<el>:>10}                                     # '      <el>'
    -{<el>:.<10}                                    # '<el>......'
    -{<el>:0}                                       # '<el>'
    +

    General Options

    {<el>:<10}                               # '<el>      '
    +{<el>:^10}                               # '   <el>   '
    +{<el>:>10}                               # '      <el>'
    +{<el>:.<10}                              # '<el>......'
    +{<el>:0}                                 # '<el>'
     
    • Options can be generated dynamically: f'{<el>:{<str/int>}[…]}'.
    • Adding '!r' before the colon converts object to string by calling its repr() method.
    -

    Strings

    {'abcde':10}                                   # 'abcde     '
    -{'abcde':10.3}                                 # 'abc       '
    -{'abcde':.3}                                   # 'abc'
    -{'abcde'!r:10}                                 # "'abcde'   "
    +

    Strings

    {'abcde':10}                             # 'abcde     '
    +{'abcde':10.3}                           # 'abc       '
    +{'abcde':.3}                             # 'abc'
    +{'abcde'!r:10}                           # "'abcde'   "
     
    -

    Numbers

    {123456:10}                                    # '    123456'
    -{123456:10,}                                   # '   123,456'
    -{123456:10_}                                   # '   123_456'
    -{123456:+10}                                   # '   +123456'
    -{123456:=+10}                                  # '+   123456'
    -{123456: }                                     # ' 123456'
    -{-123456: }                                    # '-123456'
    +

    Numbers

    {123456:10}                              # '    123456'
    +{123456:10,}                             # '   123,456'
    +{123456:10_}                             # '   123_456'
    +{123456:+10}                             # '   +123456'
    +{123456:=+10}                            # '+   123456'
    +{123456: }                               # ' 123456'
    +{-123456: }                              # '-123456'
     
    -

    Floats

    {1.23456:10.3}                                 # '      1.23'
    -{1.23456:10.3f}                                # '     1.235'
    -{1.23456:10.3e}                                # ' 1.235e+00'
    -{1.23456:10.3%}                                # '  123.456%'
    +

    Floats

    {1.23456:10.3}                           # '      1.23'
    +{1.23456:10.3f}                          # '     1.235'
    +{1.23456:10.3e}                          # ' 1.235e+00'
    +{1.23456:10.3%}                          # '  123.456%'
     

    Comparison of presentation types:

    ┏━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┓