正则表达式

正则表达式

   什么是正则表达式:描述字符串排列的一套规则。

基础知识
1.原子
  原子正则表达式中最基本的组成单位,每个正则表达式中至少要包含一个原子
(1)普通字符作为原子
(2)非打印字符作为原子 :字符串中用于格式控制的字符  如 “\n”,“\t”
 (3)通用字符作为原子:一个原子可以匹配一类字符 “\w” ,"\W","\d","\D" ,"\s","\S"
 (4)原子表:可以定义一组地位平等的原子,然后匹配的时候回取该原子表中的任意原子进行匹配。[xyz]py,[^xyz]py
    
2.元字符
  正则表达式中具有特殊意义的字符。
  (1)任意匹配元字符 :    “.”
  (2) 边界限制元字符:“^”匹配字符串的开始,“$”匹配字符串的结束
  (3)限定符:“*”、“?”、“+”、“{n}”、“{n,}”、“{n,m}”
  (4)模式选择符:“|”
  (5)模式单元符:“()”将一些原子组成一个大原子使用
3.模式修正

4.贪婪模式与懒惰模式
  贪婪模式即尽可能多的匹配,懒惰模式即尽可能少的匹配
 如想在某些字符间匹配任意字符:“p.*y”默认是贪婪模式,转化为懒惰模式“p.*?y”

正则表达式常见函数
1.re.match(pattern,string,flag)  表示从源字符串的起始位置匹配一个模式
2.re.search() 在全文中进行搜索并匹配
3.全局匹配函数:将符合模式的内容全部匹配(>=1)出来
  思路如下:(1)使用re.compile()对正则表达式进行预编译
                    (2)编译后,使用findall()根据正则表达式从源字符串中将匹配的结果全   部找出。
4.re.sub(pattern,rep,string,max) 根据正则表达式实现替换某些字符串的功能。  




  

options: -s single-line mode -m multi-line mode -i ignoreCase -G global -R from right to left -E extended-regexp pattern ERE -e ="PAT-regexp" -C ="strToOperate" -F fixed-strings fgrep -c ="num" print only a count of matching first lines per file -n force the prefixing filename on output -N without prefixing filename on output -o show only the part of the line that matched -f ="file" read regex patterns from file -q quiet suppress all normal output,stderr etc -O output file offsets, not text -x print line number with output lines -r recursively scan sub-directories -I select only non-matching lines -S ="replaceto" Match(regexp)-->replaceto -p ="bak-suffix-name",Modify file(S) directly,will create backup file(s) if bak- suffix-name is not null -d ="dir to search" default=currentDirectory,dir-str can't end with \",avoid esc ape " -X mask filename to be operated with regex -T only output filename and match num for per file search file:if set the -e arg and no (-C arg || stdin-pipe || inputfile),then ou tput filepath that match -ergx;if -o arg is set,output filenames only 从命令行中-C参数指定要处理的字符串: C:\>E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -Chello2012 -e2\d+2 Line:1 Offset:5 hello2012 //output -o参数指定只输出匹配部分: C:\>E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -Chello2012 -e2\d+2 -o 2012 从标准输入 管道里获取要处理的数据 C:\>help |E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -e^V.+?\s -o VERT VE VER VER VERIFY VOL 从文件里读取数据处理,支持递归处理目录 E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -ewindows Boo-t.ini -i Boo-t.ini result : Line:3 Offset:45 (0)partition(1)\WINDOWS [operating sys Line:5 Offset:37 (0)partition(1)\WINDOWS="Microsoft Wind Line:5 Offset:56 DOWS="Microsoft Windows XP Professional -d指定目录,-r表示递归处理 -e指定要查找的表达式,如果没有带处理数据(即没有-C\管道输入\指定文件)但指定了目录,则会 处理目录下的目录名和文件名,并输出符合表达式的路径名称 E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -eboot.i*i -i -d"c:" -r c:\boot.ini E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -eboot.i*i -i -d"c:\\"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值