[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/We2one/Notes/master/python-basic-notes/python-days/Day_18.md [Back]  [Original]

### 

### re 

1. ####  (, Regular Expression)
    +  regex / regexp
    +  () 
    
2. #### python 
    `import re`
    
3. #### 
   + 
       + patter:   
       + string:      
       + flags=0      
       + re.I   
       + re.M      
       + re.S   .  

    | 
   :---: | :---:
   re.match(patter, string) | **** string  patter  match  None
   re.search(patter, string) | **string** patter  match  None
   re.findall(patter, string) | **string**,  patter ,
   re.fullmatch(patter, string) | **string**, , None
   re.finditer(patter, string) | **string**

   + 

     ```python
     import re
      
     string = "Hello Word"
      
     res_match = re.match('He', string)
     res_search = re.search('Wo', string)
     res_findall = re.findall('o', string)
     res_full = re.fullmatch('Hello Word', string)
     res_finditer = re.finditer('o', string)
      
     print(res_match)
     print(res_match.group())
     print(res_search)
     print(res_search.group())
     print(res_findall)
     print(res_full)
     print(res_full.group())
     print(res_finditer)
     for i in res_finditer:
         print(i)
         print(i.group())
     """
     #  span  (start, end) 
     #  match  group()  group(0) 
     
     He
     
     Wo
     ['o', 'o']
     
     Hello Word
     
     
     o
     
     o
     """
     ```


4. #### 

     | 
    :---: | :---:
    re.split(reg, string) |  reg ,  string 
    re.sub(reg, repl, string) | ** repl**  **string**  **reg** 
    
    + 
    
        ```python
      import re
           
      string = "Hello Word"
            
      res_split = re.split('o', string)
      res_sub = re.sub('o', 'M', string)
            
      print(res_split)
      print(res_sub)
      """
      ['Hell', ' W', 'rd']
      HellM WMrd	
      ```


5. #### 
   
     | 
    :---: | :---:
     ^  | 
     $  | 
     .  |  (re.M )
     \d | 
     \D | 
     \s | 
     \S | 
     \w | \\
     \W | (\\)
     \b | 
     \B | 
    
    + 
    
     ```python
     import re
      
     # 
     string = '1783184424 Hello'
      
     # 
     res_1 = re.findall('.', string)
     # 
     res_2 = re.findall('\d', string)
     # 
     res_3 = re.findall('\D', string)
     # 
     res_4 = re.findall('\s', string)
     # 
     res_5 = re.findall('\S', string)
     # \\
     res_6 = re.findall('\w', string)
     # (\\)
     res_7 = re.findall('\W', string)
     # 
     res_8 = re.findall('\b', string)
     # 
     res_9 = re.findall('\B', string)
      
     print(res_1)
     print(res_2)
     print(res_3)
     print(res_4)
     print(res_5)
     print(res_6)
     print(res_7)
     print(res_8)
     print(res_9)
     """
     ['1', '7', '8', '3', '1', '8', '4', '4', '2', '4', ' ', 'H', 'e', 'l', 'l', 'o']
     ['1', '7', '8', '3', '1', '8', '4', '4', '2', '4']
     [' ', 'H', 'e', 'l', 'l', 'o']
     [' ']
     ['1', '7', '8', '3', '1', '8', '4', '4', '2', '4', 'H', 'e', 'l', 'l', 'o']
     ['1', '7', '8', '3', '1', '8', '4', '4', '2', '4', 'H', 'e', 'l', 'l', 'o']
     [' ']
     []
     ['', '', '', '', '', '', '', '', '', '', '', '', '']
     """
     ```


6. ####  ( : )

     | 
    :---: | :---:
     *  |  \*  **0**, 
     +  |  +  **1**,
     ?  |  ?  ()
     {n} |  {n}  **n** 
     {m, n} |  {m, n}  m  n 
     {n, } |  {n, }  **n** 
    
7. ####  (, )
   
     | 
    :---: | :---:
    [0-9] |  0~9   \d
    [\^0-9] |   \D
    [3-6] |  3~6 
    [a-z] |  a~z 
    [A-Z] |  A~Z 
    [a-f] |  a~f 
    [a-zA-Z] |  a~zA~Z 
    [a-zA-Z0-9] | 
    [a-zA-Z0-9_] | \\  \w
    [^a-zA-Z0-9_] | (\\)  \W
    
    + 
    
     ```python
     import re
      
     # 
     string = '17 83 18 442 4 Hello'
      
     res_1 = re.findall('[1-4]', string)
     res_2 = re.findall('[^0-9]', string)
     res_3 = re.findall('[a-z]', string)
     res_4 = re.findall('[A-Z]', string)
     res_5 = re.findall('[a-zA-Z]', string)
     res_6 = re.findall('[0-9a-zA-Z]', string)
     res_7 = re.findall('[^0-9a-zA-Z]', string)
     
     print(res_1)
     print(res_2)
     print(res_3)
     print(res_4)
     print(res_5)
     print(res_6)
     print(res_7)
     """
     ['1', '3', '1', '4', '4', '2', '4']
     [' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o']
     ['e', 'l', 'l', 'o']
     ['H']
     ['H', 'e', 'l', 'l', 'o']
     ['1', '7', '8', '3', '1', '8', '4', '4', '2', '4', 'H', 'e', 'l', 'l', 'o']
     [' ', ' ', ' ', ' ', ' ']
     """
     ```


8. ####  ( () )

    | 
   :---: | :---:
   (expression) | ;  group()  group(0) ;  1 
   (?Pexpression) |  ,  name , group(name) 

   + 

     ```python
     import re
      
     string = ''
      
     res = re.findall('.*?', string)
     res_1 = re.findall('.*?', string)
     # 
     res_2 = re.search('(.*?)', string)
     # 
     res_3 = re.search('.*?)">(?P.*?)', string)
      
     print(res)
     print(res_1)
     print(res_2)
     print(res_2.group())
     print(res_2.group(1))
     print(res_2.group(2))
      
     print(res_3)
     print(res_3.group('url'))
     print(res_3.group('content'))
     """
     ['', '']
     ['http://www.baidu.com', 'http://www.mi.com']
     
     
     http://www.baidu.com
     
     
     http://www.baidu.com
     
     """
     ```


9. #### 

    +  (+:  , **.\*** **.+** ) 

        + ****

        + 
        
    + / (**.\*?**, **.+?** )

        + 

        + 
        
          ```python
        import re
           
          string = "Hello Word"
           
          res_match = re.match('He', string)
          res_search = re.search('Wo', string)
          res_findall = re.findall('o', string)
          res_full = re.fullmatch('Hello Word', string)
          res_finditer = re.finditer('o', string)
           
          print(res_match)
          print(res_match.group())
          print(res_search)
          print(res_search.group())
          print(res_findall)
         print(res_full)
          print(res_full.group())
          print(res_finditer)
          for i in res_finditer:
              print(i)
              print(i.group())
          """
          #  span  (start, end) 
          #  match  group()  group(0) 
          
          He
          
          Wo
          ['o', 'o']
          
          Hello Word
          
          
          o
          
          o
          """
          ```
    
1. #### 
   
    1.  :  "" 
    
    2.  :  \_\_iter\_\_ 
    
    3.  :  \_\_iter\_\_  \_\_next\_\_ 
    
    4. :
        + \_\_next\_\_() : 
        + \_\_length_hint\_\_() : 
        + \_\_setstate\_\_() : 
    
2. #### for 
   
    1. for  ( \_\_iter\_\_ )
    
    2. \_\_iter\_\_  ( \_\_iter\_\_  \_\_next\_\_ )
    
    3. for  \_\_next\_\_ 
    
3. #### 
    + , python 2.7  range() 

4. #### 
    + 

      ```python
      str1 = 'Fibo'
      print(list(str1))
      print(tuple(str1))
      """
      ['F', 'i', 'b', 'o']
      ('F', 'i', 'b', 'o')
      """
      ```


    + 

        ```python
        class Fibonacci:
        
        def __init__(self, all_num):
            self.all_num = all_num
            self.a = 1
            self.b = 1
            self.current_num = 0
        
        def __iter__(self):
            #  __next__() 
            return self
        
        def __next__(self):
            if self.all_num 

Web Proxy Viewer  |  New URL  |  Original Page