正则表达式简介

本文探讨了正则表达式在Linux/UNIX系统中的应用,包括如何使用正则表达式进行字符串查找和替换,以及正则表达式的组成元素和应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   &nbsp好好努力,有一个好的将来! 日历:
var diarydays="" ; .calendarBigBorder { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; text-decoration: none; width: 170; background-color: #FFFFFF; font-size: 9pt;border:1px dotted #999; } .calendarTd { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; color: #000000; background-color: #eeeeee; height: 18px; width: 11%; text-align: center;} .calendarMInput { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; text-decoration: none; background-color: #FFFFFF; height: 15px; border: 1px solid #666666; width: 19px; color: #0099FF;} .calendarYInput { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; color: #0099FF; text-decoration: none; background-color: #FFFFFF; height: 15px; width: 34px; border: 1px solid #666666;} .calendarMonthTitle { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; font-weight: normal; height: 24px; text-align: center; color: #333333; text-decoration: none; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-bottom-style: none; border-top-color: #999999; border-right-color: #999999; border-bottom-color: #999999; border-left-color: #999999;} .calendarNow { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; font-weight: bold; color: #000000; background-color: #C0C9D3; height: 18px; text-align: center;} .calendarDaySat { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; color: #333333; text-decoration: none; background-color: #eeeeee; text-align: center; height: 18px; width: 12%;} .calendarDaySun { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; color: #333333; text-decoration: none; background-color: #eeeeee; text-align: center; height: 18px; width: 12%;} .calendarLink { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; font-weight: normal; color: #333333; text-decoration: none; background-color: #E7E7F6; text-align: center; height: 18px;} .categoryTxt { font-family: "Arial", "Helvetica", "sans-serif", "宋体"; font-size: 9pt; line-height: 23px; color: #333333; text-decoration: none;} .categoryTable { border-right-width: 1px; border-bottom-width: 1px; border-right-style: solid; border-bottom-style: solid; border-left-style: none; border-right-color: #000000; border-bottom-color: #000000; border-top-width: 1px; border-top-style: solid; border-top-color: #71b99c;} var months = new Array("一", "二", "三","四", "五", "六", "七", "八", "九","十", "十一", "十二"); var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31); var days = new Array("日","一", "二", "三","四", "五", "六"); var classTemp; var calendarHover="calendarHover"; var today=new getToday(); var year=today.year; var month=today.month; var newCal; //得到某月天数 function getDays(month, year) { if (1 == month) return ((0 == year % 4) && (0 != (year % 100))) ||(0 == year % 400) ? 29 : 28; else return daysInMonth[month]; } //得到当天时间信息 function getToday() { this.now = new Date(); this.year = this.now.getFullYear(); this.month = this.now.getMonth(); this.day = this.now.getDate(); } //生成日历 function Calendar() { newCal = new Date(year,month,1); //当前月的第一天 today = new getToday(); var day = -1; //用来判断日历中是否为当天 var startDay = newCal.getDay(); //当月开始时间 var endDay=getDays(newCal.getMonth(), newCal.getFullYear());//当月结束时间 var daily = 0; //用来生成日历中的天数值 if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth())) day = today.day; var caltable = document.all.caltable.tBodies.calendar; //得到日历表格的集合 var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear()); //得到当月天数 //生成日历 for (var intWeek = 0; intWeek "; //选择样式 if(day==daily) cell.className="calendarNow"; else if(diarydays.indexOf(d)!=-1) cell.className="calendarLink"; else if(intDay==6) cell.className = "calendarDaySat"; else if (intDay==0) cell.className ="calendarDaySun"; else cell.className="calendarTd"; //生成值 if ((daily > 0) && (daily 11) { month=0; year=year+1; } else { month=month+1; } Calendar(); } //得到响应事件 function getDiary() { var mon=(newCal.getMonth()+1)"; if ("TD" == event.srcElement.tagName) if (("" != event.srcElement.innerText)&&(diarydays.indexOf(d) !=-1)) { diary.location="/foxmail/archive/"+newCal.getFullY ear()+"/"+mon+"/"+day+".aspx"; window.location.href = diary.location; } } function setDate() { if (document.all.month.value12) { alert("月的有效范围在1-12之间!"); return; } year=Math.ceil(document.all.year.value); month=Math.ceil(document.all.month.value-1); Calendar(); } 年 月 [下月] [上月] document.write("" + days[0] + ""); for (var intLoop = 1; intLoop " + days[intLoop] + ""); document.write("" + days[intLoop] + ""); for (var intWeeks = 0; intWeeks "); for (var intDays = 0; intDays "); document.write(""); } Calendar();
  
  
  
  
  
  
  
  为什么要使用正则表达式
  linux/UNIX中提供了许多命令和工具,它们可以在文件中查找(Search)字符串或替换(Replace)字符串的功能。像grep,vi,sed,awk等,不论是查找字符串还是替换字符串,都得先告诉这些命令所要查找(被替换)的字符串是什么,如果未能事先明确知道所要查找(被替换)的字符串是什么,只知道这个字符串存在的范围或特征时,例如:(一)查找"T0.c","T1.c","T2.c"…"T9.c" 当中的任一字符串。(二)查找至少存在一个"A"的任意字符串。这种情况下,如何告诉执行查找字符串的命令所要查找的字符串是什么。例(一) 中,要查找任一在"T"与".c" 之间存在一个阿拉伯数字的字符串;当然可以用列举的方式,一一把所要查找的字符串告诉执行命令的命令。但例(二) 中符合该条件的字符串有无限种可能,势必无法一一列举。此时,便需要另一种字符串表示的方法。
  什么是正则表达式:正则表达式(以下简称Regexp)是一种字符串表达的方式。可以指定具有某特征的所有字符串。
  注:为了与一般字符串区别,在这里,在正则表达式的字符串之前皆加 "Regexp"。
  awk程序中常以/…/括住Regexp,以区别于一般字符串。
  组成正则表达式的元素:普通字符除了 . * [ ] + ? ( ) \ ^ $ 外的所有字符。由普通字符所组成的Regexp的意义与原字符串字面意义相同。例如:Regexp "the" 与一般字符串的"the" 代表相同的意义。
  (Meta character) :用来代表任意一字符。在linux/unix Shell中使用 "*"表示0个或任意长度的字符。
  在Regexp中:
  "." 代表任意一个字符
  "*" 另有其它涵意,并不代表任意长度的字符串
  ^ 表示该字符串必须出现于行首
  $ 表示该字符串必须出现于行末。
  例如:Regexp/^The/用来表示所有出现于行首的字符串"The"。Regexp/The$/ 用来表示所有出现于行末字符串"The"。
  \将特殊字符还原成字面意义的字符(Escape character) ,Regexp中特殊字符将被解释成特定的意义。如果要表示特殊字符的字面(literal meaning)意义时,在特殊字符之前加上"\"即可。例如:使用Regexp来表示字符串"a.out"时,不可写成 /a.out/,因为"."是特殊字符,表示任意一个字符。可符合Regexp / a.out/的字符串将不只 "a.out" 一个;字符串"a2out","a3out","aaout"…都符合 Regexp /a.out/, 正确的用法为:/ a\.out/。
  [...]字符集合,用来表示两中括号间所有的字符当中的任一个。
  例如:Regexp/[Tt]/可用来表示字符"T" 或 "t"。所以Regexp/[Tt]he/,表示字符串"The" 或 "the"。字符集合[...] 内不可随意留空白。例如:Regexp/[ Tt ]/,其中括号内有空白字符,除表示"T","t" 中任一个字符,也可代表一个" "(空白字符)。
  - 字符集合中可使用"-" 来指定字符的区间,其用法如下:Regexp /[0-9]/等于/[0123456789]/,用来表示任意一个阿拉伯数字。同理Regexp/[A-Z]/ 用来表示任意一个大写英文字母。但要注意,Regexp /[0-9a-z]/并不等于/[0-9][a-z]/;前者表示一个字符,后者表示二个字符。Regexp/[-9]/或/[9-]/只代表字符,"9″或 "-"。
  [^...]使用[^...] 产生字符集合的补集(complement set)。其用法如下:例如:要指定"T"或"t"之外的任一个字符,可用/[^Tt]/表示。同理Regexp/[^a-zA-Z]/,表示英文字母之外的任一个字符。
  注意"^" 的位置:"^"必须紧接在"["之后,才代表字符集合的补集。例如:Regexp /[0-9\^]/只是用来表示一个阿拉伯数字或字符"^"。
  * 表示字符重复次数的特殊字符。"*" 表示它前方之字符可出现0次或任意多次,即字符大于等于0次。例如:Regexp/T[0-9]*\.c/中,*表示其前[0-9](一个阿拉伯数字)出现的次数可为0次或多次。所以Regexp /T[0-9]*\.c/可用来表示"T.c","T0.c","T1.c"…"T19.c" 。
  +表示其前的字符出现一次或一次以上。例如:Regexp /[0-9]+/ 用来表示一位或一位以上的数字。
  ? 表示其前的字符可出现一次或不出现。例如:Regexp /[+-]?[0-9]+/ 表示数字(一位以上)之前可出现正负号或不出现正负号。
  (…)用来括住一群字符,且把他当成一个group。例如:Regexp /12+/ 表示字符串"12″,"122″,"1222″,"12222″,…。egexp /(12)+/ 表示字符串 "12″,"1212″,"121212″,"12121212″…。上式中12 以( )括住,所以"+" 所表示的是12,重复出现的也是12。
  | 表示逻辑上的"或"(or) 。例如:Regexp/ Oranges?|apples?|water/可用来表示:字符串 "Orange","Oranges" 或 "apple","apples" 或 "water" 。
  match是什么? 讨论Regexp时,经常遇到"某字符串匹配( match )某Regexp"的字眼。意思是: "这个Regexp可被解释成该字符串"。[ 例如]: 字符串"the" 匹配(match) Regexp /[Tt]he/。因为 Regexp /[Tt]he/ 可解释成字符串 "the" 或 "The",故字符串 "the" 或 "The"都匹配(match) Regexp /[Th]he/。
  awk 中提供二个关系运算符(Relational Operator):~ ,!~,它们也称之为match,not match。但函义与一般常说的match略有不同。其定义如下:A 表一字符串,B 表一Regular Expression,只要A 字符串中存在有子字符串可match( 一般定义的 match) Regexp B ,则A ~B 就算成立,其值为true,反之则为false。! ~ 的定义与~恰好相反。例如:"another" 中含有子字符串 "the" 可match Regexp /[Tt]he/,所以 "another" ~ /[Tt]he/ 之值为 true
  有些地方不把( ~,!~)与Relational Operators 归为一类。
  应用Regular Expression 解题的简例:下面列出一些应用Regular Expression 的简例
  例1:将文件中所有的字符串 "Regular Expression" 或 "Regular expression" 换成 "Regexp"
  # cat regexp
  Regular expression
  Regular Expression
  # awk '{gsub(/Regular[ \t]+[eE]xpression/,"Regexp");print $0;}' regexp
  Regexp
  Regexp
  例2:去除文件中的空白行(或仅含空白字符或tab 的行)
  # cat regexp
  Regular expression
  Regular Expression
  # awk '{if($0!~/^[ \t]+$/) print $0;}' regexp
  Regular expression
  Regular Expression
  例3:在文件中具有ddd-dddd (电话号码型态,d 表digital)的字符串前加上"TEL :
  # cat regexp
  83786550
  83786450
  # awk '{gsub(/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/, "TEL: &");print $0;}' regexp
  TEL: 83786550
  TEL: 83786450
  例4:从文件的Fullname 中分离出路径与文件名
  # awk 'BEGIN{Fullname="/usr/local/apache2/bin/apachectl" ;match(Fullname,/.*\//);path=substr(Fullname,1,RLEN GTH-1);filename=substr(Fullname,RLENGTH+1);print Fullname;print path;print filename}'
  /usr/local/apache2/bin/apachectl
  /usr/local/apache2/bin
  apachectl
  例5:将某一数值改以现金表示法表示(整数部分每三位加一撇,且含二位小数)
  # awk 'BEGIN{number=1234567890;number=sprintf("¥%.2f",number);while(match(number,/[0-9][0-9][0-9][ 0-9]/)) sub(/[0-9][0-9][0-9][.,]/,",&",number); print number}'
  结果输出 ¥1,234,567,890.00
  例6: 把文件中所有具"program数字.f"形态的字符串改为"[Ref :program数字.c]"
  # cat program
  program1.f
  program2.f
  program34.f
  program67.f
  # awk '{while(match($0,/program[0-9]+\.f/)){replace="[Re f: "substr($0,RSTART,RLENGTH-2)".c]"; sub(/program[0-9]+\.f/,replace);} print $0}' program
  [Ref: program1.c]
  [Ref: program2.c]
  [Ref: program34.c]
  [Ref: program67.c]
  解释说明:以program1.f为例,while(match($0,/program[0-9]+\.f/))是匹配以下"program1.f"的文件名,匹配后结果会保存到RSTART=0和RLENGTH=10中;substr($0,RSTART,RLENGTH-2)就是去program1.f的前8位,即program1;replace="[Ref: "substr($0,RSTART,RLENGTH-2)".c]"的结果就是replace="[Ref: program1.c]";sub函数是用来进行代替的,也就是用[Ref: program1.c]代替program1.f。所以输出结果为上式的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值