python字符串的正则匹配:re模块

发布时间:2022-04-21 09:56:52 人气:236 作者:多测师

  正则表达式在匹配负责字符串的时候,确实很有用:

  >>> import re

  >>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')

  ['foot', 'fell', 'fastest']

  >>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')

  'cat in the hat'

  但是,如果是简单的对字符串进行操作,官网推荐的还是使用string模块的函数,因为这些函数看起来更易读更简洁,更容易调试:

  >>> 'tea for too'.replace('too', 'two')

  'tea for two'

python字符串的正则匹配:re模块

  互联网访问:urllib2模块

  用于访问互联网以及处理网络通信协议中,最简单的两个是用于处理从 urls 接收的数据的 urllib2 以及用于发送电子邮件的 smtplib。

  最简单的例子:

  import urllib2

  response = rllib2.urlopen('http://python.org/')

  html = response.read()

  日期和时间:datetime 模块

  datetime 模块为日期和时间处理同时提供了简单和复杂的方法。支持日期和时间算法的同时,实现的重点放在更有效的处理和格式化输出。该模块还支持时区处理,函数方法比较简单,就直接从官网截取例子:

  >>> # dates are easily constructed and formatted

  >>> from datetime import date

  >>> now = date.today()

  >>> now

  datetime.date(2003, 12, 2)

  >>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")

  '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'

  >>> # dates support calendar arithmetic

  >>> birthday = date(1964, 7, 31)

  >>> age = now - birthday

  >>> age.days

  14368

  以上内容为大家介绍了python字符串的正则匹配:re模块,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线