python 之re模块

上一篇 / 下一篇  2018-03-25 10:26:49 / 个人分类: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) 



TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2018-04-24  
1234567
891011121314
15161718192021
22232425262728
2930     

数据统计

  • 访问量: 3250
  • 日志数: 48
  • 建立时间: 2018-01-19
  • 更新时间: 2018-04-07

RSS订阅

Open Toolbar
博聚网