ghong6003的个人空间 https://blog.eetop.cn/1430538 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

python 之re模块

已有 831 次阅读| 2018-3-25 10:26 |个人分类:Python|系统分类:芯片设计

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ************************************************************
# File Name   : reExe.py
# Author      : Hong Guo
# Description : This file used to try to parse options from the command parameters
# From        :
# ************************************************************

import sys
#import xlrd
#import xlwt
import csv
import os
import re
import traceback
from optparse import OptionParser

def main():
    fullStr="Be the one you wanna to be!!"   # The string to be match
    matchStr="wan+"                          # The match item string
    matchPat=re.compile(matchStr)            # The match item pattern
    print("matchPat is " + str(matchPat))  

    res=  re.match('Be',fullStr)             # Try to match string from the beginning.
    res2=  matchPat.search(fullStr)             # Try to match string from the beginning.
    if res:
        print('Matched the string' + "The full string is " + res.string)

    res3=  matchPat.search(fullStr)             # Try to match string from the beginning.
    if re.search(matchPat,fullStr):          # Search the pattern
        print("Found it")
    
    print(re.split('[ ]',fullStr,maxsplit=2)) # Split string based on the pattern
    print(re.split(matchPat,fullStr,maxsplit=2)) # Split string based on the pattern

    pat='[a-zA-Z]+'
    test='Er..hello,,.you are ___ so !! bueatifull!'
    print(re.findall(pat,test))              # ['Er', 'hello', 'you', 'are', 'so', 'bueatifull']

    print(re.sub(matchStr,"Want",fullStr))   # Be the one you Wanta to be!!

    print(re.escape('wwww.baidu.com'))       # wwww\.baidu\.com

    # Group for match
    #m = re.match(r'www\.(.*)\..{3}','www.python.org')
    m = re.search(r'ww\.(.*)\..{3}','www.python.org')
    print(m.group(0))   # Get the full string
    print(m.group(1))   # Get the first group
    print(m.start(1))   # Get the start index of the full string
    print(m.end(1))     # Get the end index of the full string
    print(m.span(1))    # Get the tuple of both begin and end

    # More info for the pattern gen
    emphasisPat=r'\*([^\*]+)\*'
    emphasisPat2=re.compile(r'''
                            \*       # fisrt elments
                            (        # begin group 
                            [^\*]+   # group match value
                            )        # end group
                            \*       # last elments
                            ''', re.VERBOSE )
    print(emphasisPat2.sub(r'<em>\1<em>','Hello *World*!'))
    print(re.sub(emphasisPat2,r'<em>\1<em>','Hello *World*!'))

if __name__ == '__main__':
    try:
        main()
    except (StandardError, ValueError):
        stackTrace = traceback.format_exc()
        sys.stdout.write(stackTrace)
        sys.exit(1) 



点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 1

    关注
  • 3

    粉丝
  • 0

    好友
  • 2

    获赞
  • 1

    评论
  • 1539

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-28 00:04 , Processed in 0.020690 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部