The use of regular expressions python

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 17 19:29:44 2019

@author: xbxia
"""
import random
import re

#按规则生成指定数量的sn号
def save(count):
    
    with open('devSn.txt', 'a+') as fw_t:
        for i in range(0, count):
            devSn = ('hongdian'+"%08d"%(i))
            print(devSn)
            fw_t.write(devSn)
            fw_t.write ( ' \ n- ' ) 
            
 
# regular expression 
# from the initial position matching digits 
DEF the match_pattern (Content, Start, End): 
    pattern = the re.compile (R & lt ' \ + D ' ) 
    result_obj = pattern.match (Content , Start, End)   
    Result = result_obj.group ()     # remove content that matches 
    # Print (Result) 
    return Result 

# to find out the number of strings 
DEF findall_pattern (content): 
    pattern = the re.compile ( ' hongdian ' ) 
    Result = pattern.findall (content)
     #Print (Result) 
    return len (Result) 

# specified position search the entire string, but only returns the first matching result 
DEF search_pattern (Content, POS): 
    pattern = the re.compile ( ' hongdian ' ) 
    Result = pattern.search (Content , POS)
     Print (Result) 
    



            
IF  the __name__ == ' __main__ ' :
     # Save (100) 
    # the match_pattern (Content, 20, 22) 
    Content = ' h88dhonghhhhhongdian000hongdian ' 
    search_pattern (Content, 22 is )
   
    
    

 

Guess you like

Origin www.cnblogs.com/relustarry/p/12588529.html