File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -24,5 +24,7 @@ The following code is something really useful.
2424
2525** 【011】GenerateUniqueCode.py——生成唯一随机码(可作为优惠券或验证码等)** 
2626
27+ ** 【012】Re_Basic.py——re正则表达式基本使用** 
28+ 
2729
2830......持续更新中
Original file line number Diff line number Diff line change 1+ import  re 
2+ 
3+ # 正则表达式最好编译一下可复用 
4+ # search方法比match通用 
5+ # findall查找所用匹配项目比较方便 
6+ 
7+ html  =  r"<html><body><p>hello world</p><p>hello world2</p><p>hello world3</p></body></html>" 
8+ rule  =  r"<p>(.*?)</p>"   # .*?:任意字符+零个或多个+非贪婪模式 
9+ pattern  =  re .compile (rule )
10+ group_result  =  pattern .search (html )  # 得到一个p标签,无论这个p标签在什么地方 
11+ print (group_result .group (1 ))  # 得到分组1的结果:hello world,如果分组值为0或者不传,则结果:<p>hello world</p> 
12+ 
13+ group_result2  =  pattern .match (html )  # 得到一个p标签,这个p标签必须在文本开头,不然结果为None 
14+ print (group_result2 )  # 因为没有p标签开头的东东,所以为:None 
15+ 
16+ list_result  =  pattern .findall (html )  # 得到所有p标签的内容,返回的是个字符串列表 
17+ print (list_result )  # ['hello world', 'hello world2', 'hello world3'] 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments