#!/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)