对于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()

到此这篇关于python regex库实例用法总结的文章就介绍到这了,更多相关python regex库是什么内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!