python判断句子是否匹配某种模式

 

re.search是字符串里面存在某种正则

if  re.search(r"^\s{0,}\(([a-zA-Z]|\d+)\)", en):    
    lines.append(ch + '|||' + en +'\n')

  

re.match是整个字符串匹配

if re.match(r".*\|\|\|.*", line):

 

re.findall可能得到的是数组,len(re.findall)判断数组是否为空 即能判断字符串中是否匹配某种模式

 while len(re.findall( r'[a-z]+[A-Z]',  line)) > 0:

 

如何拆分TomJackSun为Tom Jack Sun:

 while len(re.findall( r'[a-z]+[A-Z]',  line)) > 0: #TomJack->Tom Jack
        line = re.sub( r'([a-z]+)([A-Z])', r'\1 \2', line )

 

替换操作re.sub和re.subn有什么区别呢:

把?换为!  nu代表替换了几个

line,nu = re.subn(r'!','!',line)   

 

把空格替换掉

ch = re.sub('\s+','',ch)

 

posted @ 2017-12-15 15:33  hozhangel  阅读(2295)  评论(0编辑  收藏  举报