python regex库实例用法总结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python regex库实例用法总结

十一   2021-01-03 我要评论
在本篇内容里小编给大家整理了关于python regex库实例用法总结内容,有需要的朋友们参考学习下。

对于regex库的使用不难,因为本身就是python中自带的库,所以在调用上也是常见的库使用类型,大部分时候都是用于搜索上下文信息的,但是有些时候也会调用它的两个使用方法,其中一个是编译,另外一个是匹配,能够进行匹配的对象有很多,比如字符串,单一的字符等等,好啦,下面来详细看下使用吧。

调用实例:

from uregex import Regex_input
x=Regex_input('j','jd')
x.regex()
c=Regex_input('j','d')
c.regex()

编译实例:

for regex in regexes:
  print 'seeking "%s" ->' % regex.pattern
  if regex.search(text):
    print 'match'
  else:
    print 'No match'

这两种方式都是常见的项目应用实例,大家可以浏览掌握住,对我们的项目实例还是非常有帮助的

Python Regex库的使用实例扩展

#!/usr/bin/python
 
import re
 
class Regex_input:
  def __init__(self,task,source):
    self.source=source
    self.task=task
  def regex(self):
    opt=re.search(self.task,self.source)
    if opt==None:
      print 'No Found the task: %s' % self.task
    else:
      print '%s is in %d - %d' % (self.task,opt.start(),opt.end())

调用实例:

from uregex import Regex_input
 
x=Regex_input('j','jd')
x.regex()
c=Regex_input('j','d')
c.regex()

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们