- 浏览: 81260 次
- 性别:
- 来自: 金华
最新评论
正则表达式1
只能输入数字:"^[0-9]*$"。
只能输入n位的数字:"^\d{n}$"。
只能输入至少n位的数字:"^\d{n,}$"。
只能输入m~n位的数字:。"^\d{m,n}$"
只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。
只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。
只能输入非零的正整数:"^\+?[1-9][0-9]*$"。
只能输入非零的负整数:"^\-[1-9][]0-9"*$。
只能输入长度为3的字符:"^.{3}$"。
只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。
只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。
只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。
只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。
只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"。
验证用户密码:"^[a-zA-Z]\w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。
验证是否含有^%&',;=?$\"等字符:"[^%&',;=?$\x22]+"。
只能输入汉字:"^[\u4e00-\u9fa5]{0,}$"
验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。
验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
验证电话号码:"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。
验证身份证号(15位或18位数字):"^\d{15}|\d{18}$"。
验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。
验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。
匹配中文字符的正则表达式:<wbr><span style="font-family:Tahoma">[\u4e00-\u9fa5]</span></wbr>
匹配双字节字符(包括汉字在内):[^\x00-\xff]
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
String.prototype.len=function(){return<wbr>this.replace(/[^\x00-\xff]/g,"aa").length;}</wbr>
匹配空行的正则表达式:\n[\s|<wbr>]*\r</wbr>
匹配html标签的正则表达式:<(.*)>(.*)<\/(.*)>|<(.*)\/>
匹配首尾空格的正则表达式:(^\s*)|(\s*$)
应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:
String.prototype.trim<wbr>=<wbr>function()</wbr></wbr>
{
return<wbr>this.replace(/(^\s*)|(\s*$)/g,<wbr>"");</wbr></wbr>
}
利用正则表达式分解和转换IP地址:
下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序:
function<wbr>IP2V(ip)</wbr>
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g<wbr>//<span style="font-family:宋体">匹配</span><span style="font-family:Tahoma">IP</span><span style="font-family:宋体">地址的正则表达式</span></wbr>
if(re.test(ip))
{
return<wbr>RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1</wbr>
}
else
{
throw<wbr>new<wbr>Error("Not<wbr>a<wbr>valid<wbr>IP<wbr>address!")</wbr></wbr></wbr></wbr></wbr></wbr>
}
}
不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:
var<wbr>ip="10.100.20.168"</wbr>
ip=ip.split(".")
alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w-<wbr>./?%&=]*)?</wbr>
利用正则表达式限制网页表单里的文本框输入内容:
用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')"<wbr>onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"</wbr>
用正则表达式限制只能输入全角字符:<wbr><span style="font-family:Tahoma">onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')"<wbr>onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"</wbr></span></wbr>
用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'')<wbr>"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"</wbr>
用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'')<wbr>"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"</wbr>
<input<wbr>onkeyup="value=value.replace(/[^\u4E00-\u9FA5\w]/g,'')"<wbr>onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5\w]/g,''))"<wbr>value="<span style="font-family:宋体">允许下划线</span><span style="font-family:Tahoma">,</span><span style="font-family:宋体">数字字母和汉字</span><span style="font-family:Tahoma">"></span></wbr></wbr></wbr>
<script<wbr>language="javascript"></wbr>
if<wbr>(document.layers)//<span style="font-family:宋体">触发键盘事件</span></wbr>
document.captureEvents(Event.KEYPRESS)
function<wbr>xz(thsv,nob){</wbr>
if(nob=="2"){
window.clipboardData.setData("text","")
alert("避免非法字符输入,请勿复制字符");
return<wbr>false;</wbr>
}
if<wbr>(event.keyCode!=8<wbr>&&<wbr>event.keyCode!=16<wbr>&&<wbr>event.keyCode!=37<wbr>&&<wbr>event.keyCode!=38<wbr>&&<wbr>event.keyCode!=39<wbr>&&<wbr>event.keyCode!=40){</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
thsvv=thsv.value;//输入的值
thsvs=thsvv.substring(thsvv.length-1);//输入的最后一个字符
//thsvss=thsvv.substring(0,thsvv.length-1);//去掉最后一个错误字符
if<wbr>(!thsvs.replace(/[^\u4E00-\u9FA5\w]/g,'')<wbr>||<wbr>event.keyCode==189){//<span style="font-family:宋体">正则除去符号和下划线<wbr></wbr></span><span style="font-family:Tahoma">key</span></wbr></wbr></wbr>
thsv.value='请勿输入非法符号<wbr></wbr>['+thsvs+']';
alert('请勿输入非法符号<wbr></wbr>['+thsvs+']');
thsv.value="";
return<wbr>false;</wbr>
}
}
}
</script>
<input<wbr>onkeyup="xz(this,1)"<wbr>onPaste="xz(this,2)"<wbr>value=""><span style="font-family:宋体">允许数字字母和汉字</span></wbr></wbr></wbr>
<script<wbr>language="javascript"></wbr>
<!--
function<wbr>MaxLength(field,maxlimit){</wbr>
var<wbr>j<wbr>=<wbr>field.value.replace(/[^\x00-\xff]/g,"**").length;</wbr></wbr></wbr>
//alert(j);
var<wbr>tempString=field.value;</wbr>
var<wbr>tt="";</wbr>
if(j<wbr>><wbr>maxlimit){</wbr></wbr>
for(var<wbr>i=0;i<maxlimit;i++){</wbr>
if(tt.replace(/[^\x00-\xff]/g,"**").length<wbr><<wbr>maxlimit)</wbr></wbr>
tt<wbr>=<wbr>tempString.substr(0,i+1);</wbr></wbr>
else
break;
}
if(tt.replace(/[^\x00-\xff]/g,"**").length<wbr>><wbr>maxlimit)</wbr></wbr>
tt=tt.substr(0,tt.length-1);
field.value<wbr>=<wbr>tt;</wbr></wbr>
}else{
;
}
}
</script>
单行文本框控制<br<wbr>/></wbr>
<INPUT<wbr>type="text"<wbr>id="Text1"<wbr>name="Text1"<wbr>onpropertychange="MaxLength(this,<wbr>5)"><br<wbr>/></wbr></wbr></wbr></wbr></wbr></wbr>
多行文本框控制:<br<wbr>/></wbr>
<TEXTAREA<wbr>rows="14"</wbr>
cols="39"<wbr>id="Textarea1"<wbr>name="Textarea1"<wbr>onpropertychange="MaxLength(this,<wbr>15)"></TEXTAREA><br<wbr>/></wbr></wbr></wbr></wbr></wbr>
控制表单内容只能输入数字,中文....
<script>
function<wbr>test()<wbr><wbr></wbr></wbr></wbr>
{
if(document.a.b.value.length>50)
{
alert("不能超过50个字符!");
document.a.b.focus();
return<wbr>false;</wbr>
}
}
</script>
<form<wbr>name=a<wbr>onsubmit="return<wbr>test()"></wbr></wbr></wbr>
<textarea<wbr>name="b"<wbr>cols="40"<wbr>wrap="VIRTUAL"<wbr>rows="6"></textarea></wbr></wbr></wbr></wbr>
<input<wbr>type="submit"<wbr>name="Submit"<wbr>value="check"></wbr></wbr></wbr>
</form>
只能是汉字
<input<wbr>onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')"></wbr>
只能是英文字符
<script<wbr>language=javascript></wbr>
function<wbr>onlyEng()</wbr>
{
if(!(event.keyCode>=65&&event.keyCode<=90))
<wbr><wbr><wbr><wbr>event.returnValue=false;</wbr></wbr></wbr></wbr>
}
</script>
<input<wbr>onkeydown="onlyEng();"></wbr>
<input<wbr>name="coname"<wbr>type="text"<wbr>size="50"<wbr>maxlength="35"<wbr>class="input2"<wbr>onkeyup="value=value.replace(/[\W]/g,'')<wbr>"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
只能是数字
<script<wbr>language=javascript></wbr>
function<wbr>onlyNum()</wbr>
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
//考虑小键盘上的数字键
<wbr><wbr><wbr><wbr>event.returnValue=false;</wbr></wbr></wbr></wbr>
}
</script>
<input<wbr>onkeydown="onlyNum();"></wbr>
只能是英文字符和数字
<input<wbr>onkeyup="value=value.replace(/[\W]/g,'')<wbr>"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"></wbr></wbr>
验证为email格式
<SCRIPT<wbr>LANGUAGE=Javascript<wbr>RUNAT=Server></wbr></wbr>
function<wbr>isEmail(strEmail)<wbr>{</wbr></wbr>
if<wbr>(strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)<wbr>!=<wbr>-1)</wbr></wbr></wbr>
return<wbr>true;</wbr>
else
alert("oh");
}
</SCRIPT>
<input<wbr>type=text<wbr>onblur=isEmail(this.value)></wbr></wbr>
屏蔽关键字(sex<wbr>,<wbr>fuck)<wbr>-<wbr></wbr></wbr></wbr></wbr>已修改
<script<wbr>language="JavaScript1.2"></wbr>
function<wbr>test()<wbr>{</wbr></wbr>
if((a.b.value.indexOf<wbr>("sex")<wbr>==<wbr>0)||(a.b.value.indexOf<wbr>("fuck")<wbr>==<wbr>0)){</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>alert("<span style="font-family:宋体">五讲四美三热爱</span><span style="font-family:Tahoma">");</span></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>a.b.focus();</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>return<wbr>false;}</wbr></wbr></wbr></wbr></wbr>
}
</script>
<form<wbr>name=a<wbr>onsubmit="return<wbr>test()"></wbr></wbr></wbr>
<input<wbr>type=text<wbr>name=b></wbr></wbr>
<input<wbr>type="submit"<wbr>name="Submit"<wbr>value="check"></wbr></wbr></wbr>
</form>
限制文本框里只能输入数字
<input<wbr>onkeyup="if(event.keyCode<wbr>!=37<wbr>&&<wbr>event.keyCode<wbr>!=<wbr>39)<wbr>value=value.replace(/\D/g,'');"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/\D/g,''))"><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<PIXTEL_MMI_EBOOK_2005>2<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></PIXTEL_MMI_EBOOK_2005></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
JAVA正则表达式语法(转)
正则表达式语法
正则表达式是一种文本模式,包括普通字符(例如,a<wbr></wbr>到<wbr></wbr>z<wbr></wbr>之间的字母)和特殊字符(称为“元字符”)。模式描述在搜索文本时要匹配的一个或多个字符串。
正则表达式示例
表达式<wbr>匹配<wbr></wbr></wbr>
/^\s*$/
匹配空行。
/\d{2}-\d{5}/
验证由两位数字、一个连字符再加<wbr><span style="font-family:Arial">5<wbr></wbr></span><span style="font-family:宋体">位数字组成的<wbr></wbr></span><span style="font-family:Arial">ID<wbr></wbr></span><span style="font-family:宋体">号。</span></wbr>
/<\s*(\S+)(\s[^>]*)?>[\s\S]*<\s*\/\1\s*>/
匹配<wbr><span style="font-family:Arial">HTML<wbr></wbr></span><span style="font-family:宋体">标记。</span></wbr>
下表包含了元字符的完整列表以及它们在正则表达式上下文中的行为:
字符<wbr>说明<wbr></wbr></wbr>
\
将下一字符标记为特殊字符、文本、反向引用或八进制转义符。例如,“n”匹配字符“n”。“\n”匹配换行符。序列“\\”匹配“\”,“\(”匹配“(”。
^
匹配输入字符串开始的位置。如果设置了<wbr><span style="font-family:Arial">RegExp<wbr></wbr></span><span style="font-family:宋体">对象的<wbr></wbr></span><span style="font-family:Arial">Multiline<wbr></wbr></span><span style="font-family:宋体">属性,</span><span style="font-family:Arial">^<wbr></wbr></span><span style="font-family:宋体">还会与</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\n</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">或</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\r</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">之后的位置匹配。</span></wbr>
$
匹配输入字符串结尾的位置。如果设置了<wbr><span style="font-family:Arial">RegExp<wbr></wbr></span><span style="font-family:宋体">对象的<wbr></wbr></span><span style="font-family:Arial">Multiline<wbr></wbr></span><span style="font-family:宋体">属性,</span><span style="font-family:Arial">$<wbr></wbr></span><span style="font-family:宋体">还会与</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\n</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">或</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\r</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">之前的位置匹配。</span></wbr>
*
零次或多次匹配前面的字符或子表达式。例如,zo*<wbr></wbr>匹配“z”和“zoo”。*<wbr></wbr>等效于<wbr></wbr>{0,}。
+
一次或多次匹配前面的字符或子表达式。例如,“zo+”与“zo”和“zoo”匹配,但与“z”不匹配。+<wbr></wbr>等效于<wbr></wbr>{1,}。
?
零次或一次匹配前面的字符或子表达式。例如,“do(es)?”匹配“do”或“does”中的“do”。?<wbr></wbr>等效于<wbr></wbr>{0,1}。
{n}
n<wbr><span style="font-family:宋体">是非负整数。正好匹配<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">次。例如,</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o{2}</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">与</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Bob</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">不匹配,但与</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">food</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的两个</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">匹配。</span></wbr>
{n,}
n<wbr><span style="font-family:宋体">是非负整数。至少匹配<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">次。例如,</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o{2,}</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">不匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Bob</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">,而匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">foooood</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的所有<wbr></wbr></span><span style="font-family:Arial">o</span><span style="font-family:宋体">。</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o{1,}</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">等效于</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o+</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">。</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o{0,}</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">等效于</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o*</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">。</span></wbr>
{n,m}
M<wbr><span style="font-family:宋体">和<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">是非负整数,其中<wbr></wbr></span><span style="font-family:Arial">n<wbr><=<wbr>m</wbr></wbr></span><span style="font-family:宋体">。匹配至少<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">次,至多<wbr></wbr></span><span style="font-family:Arial">m<wbr></wbr></span><span style="font-family:宋体">次。例如,</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">o{1,3}</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">fooooood</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的头三个<wbr></wbr></span><span style="font-family:Arial">o</span><span style="font-family:宋体">。</span><span style="font-family:Arial">'o{0,1}'<wbr></wbr></span><span style="font-family:宋体">等效于<wbr></wbr></span><span style="font-family:Arial">'o?'</span><span style="font-family:宋体">。注意:您不能将空格插入逗号和数字之间。</span></wbr>
?
当此字符紧随任何其他限定符(*、+、?、{n}、{n,}、{n,m})之后时,匹配模式是“非贪心的”。“非贪心的”模式匹配搜索到的、尽可能短的字符串,而默认的“贪心的”模式匹配搜索到的、尽可能长的字符串。例如,在字符串“oooo”中,“o+?”只匹配单个“o”,而“o+”匹配所有“o”。
.
匹配除“\n”之外的任何单个字符。若要匹配包括“\n”在内的任意字符,请使用诸如“[\s\S]”之类的模式。
(pattern)
匹配<wbr><span style="font-family:Arial">pattern<wbr></wbr></span><span style="font-family:宋体">并捕获该匹配的子表达式。可以使用<wbr></wbr></span><span style="font-family:Arial">$0</span><span style="font-family:Times New Roman">…</span><span style="font-family:Arial">$9<wbr></wbr></span><span style="font-family:宋体">属性从结果</span><span style="font-family:Times New Roman">“</span><span style="font-family:宋体">匹配</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">集合中检索捕获的匹配。若要匹配括号字符<wbr></wbr></span><span style="font-family:Arial">(<wbr>)</wbr></span><span style="font-family:宋体">,请使用</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\(</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">或者</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\)</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">。</span></wbr>
(?:pattern)
匹配<wbr><span style="font-family:Arial">pattern<wbr></wbr></span><span style="font-family:宋体">但不捕获该匹配的子表达式,即它是一个非捕获匹配,不存储供以后使用的匹配。这对于用</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">or</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">字符<wbr></wbr></span><span style="font-family:Arial">(|)<wbr></wbr></span><span style="font-family:宋体">组合模式部件的情况很有用。例如,</span><span style="font-family:Arial">'industr(?:y|ies)<wbr></wbr></span><span style="font-family:宋体">是比<wbr></wbr></span><span style="font-family:Arial">'industry|industries'<wbr></wbr></span><span style="font-family:宋体">更经济的表达式。</span></wbr>
(?=pattern)
执行正向预测先行搜索的子表达式,该表达式匹配处于匹配<wbr><span style="font-family:Arial">pattern<wbr></wbr></span><span style="font-family:宋体">的字符串的起始点的字符串。它是一个非捕获匹配,即不能捕获供以后使用的匹配。例如,</span><span style="font-family:Arial">'Windows<wbr>(?=95|98|NT|2000)'<wbr></wbr></wbr></span><span style="font-family:宋体">匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Windows<wbr>2000</wbr></span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Windows</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">,但不匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Windows<wbr>3.1</wbr></span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Windows</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">。预测先行不占用字符,即发生匹配后,下一匹配的搜索紧随上一匹配之后,而不是在组成预测先行的字符后。</span></wbr>
(?!pattern)
执行反向预测先行搜索的子表达式,该表达式匹配不处于匹配<wbr><span style="font-family:Arial">pattern<wbr></wbr></span><span style="font-family:宋体">的字符串的起始点的搜索字符串。它是一个非捕获匹配,即不能捕获供以后使用的匹配。例如,</span><span style="font-family:Arial">'Windows<wbr>(?!95|98|NT|2000)'<wbr></wbr></wbr></span><span style="font-family:宋体">匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Windows<wbr>3.1</wbr></span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的<wbr></wbr></span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Windows</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">,但不匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Windows<wbr>2000</wbr></span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">中的</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">Windows</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">。预测先行不占用字符,即发生匹配后,下一匹配的搜索紧随上一匹配之后,而不是在组成预测先行的字符后。</span></wbr>
x|y
匹配<wbr><span style="font-family:Arial">x<wbr></wbr></span><span style="font-family:宋体">或<wbr></wbr></span><span style="font-family:Arial">y</span><span style="font-family:宋体">。例如,</span><span style="font-family:Arial">'z|food'<wbr></wbr></span><span style="font-family:宋体">匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">z</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">或</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">food</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">。</span><span style="font-family:Arial">'(z|f)ood'<wbr></wbr></span><span style="font-family:宋体">匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">zood</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">或</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">food</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">。</span></wbr>
[xyz]
字符集。匹配包含的任一字符。例如,“[abc]”匹配“plain”中的“a”。
[^xyz]
反向字符集。匹配未包含的任何字符。例如,“[^abc]”匹配“plain”中的“p”。
[a-z]
字符范围。匹配指定范围内的任何字符。例如,“[a-z]”匹配“a”到“z”范围内的任何小写字母。
[^a-z]
反向范围字符。匹配不在指定的范围内的任何字符。例如,“[^a-z]”匹配任何不在“a”到“z”范围内的任何字符。
\b
匹配一个字边界,即字与空格间的位置。例如,“er\b”匹配“never”中的“er”,但不匹配“verb”中的“er”。
\B
非字边界匹配。“er\B”匹配“verb”中的“er”,但不匹配“never”中的“er”。
\cx
匹配<wbr><span style="font-family:Arial">x<wbr></wbr></span><span style="font-family:宋体">指示的控制字符。例如,</span><span style="font-family:Arial">\cM<wbr></wbr></span><span style="font-family:宋体">匹配<wbr></wbr></span><span style="font-family:Arial">Control-M<wbr></wbr></span><span style="font-family:宋体">或回车符。</span><span style="font-family:Arial">x<wbr></wbr></span><span style="font-family:宋体">的值必须在<wbr></wbr></span><span style="font-family:Arial">A-Z<wbr></wbr></span><span style="font-family:宋体">或<wbr></wbr></span><span style="font-family:Arial">a-z<wbr></wbr></span><span style="font-family:宋体">之间。如果不是这样,则假定<wbr></wbr></span><span style="font-family:Arial">c<wbr></wbr></span><span style="font-family:宋体">就是</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">c</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">字符本身。</span></wbr>
\d
数字字符匹配。等效于<wbr><span style="font-family:Arial">[0-9]</span><span style="font-family:宋体">。</span></wbr>
\D
非数字字符匹配。等效于<wbr><span style="font-family:Arial">[^0-9]</span><span style="font-family:宋体">。</span></wbr>
\f
换页符匹配。等效于<wbr><span style="font-family:Arial">\x0c<wbr></wbr></span><span style="font-family:宋体">和<wbr></wbr></span><span style="font-family:Arial">\cL</span><span style="font-family:宋体">。</span></wbr>
\n
换行符匹配。等效于<wbr><span style="font-family:Arial">\x0a<wbr></wbr></span><span style="font-family:宋体">和<wbr></wbr></span><span style="font-family:Arial">\cJ</span><span style="font-family:宋体">。</span></wbr>
\r
匹配一个回车符。等效于<wbr><span style="font-family:Arial">\x0d<wbr></wbr></span><span style="font-family:宋体">和<wbr></wbr></span><span style="font-family:Arial">\cM</span><span style="font-family:宋体">。</span></wbr>
\s
匹配任何空白字符,包括空格、制表符、换页符等。与<wbr><span style="font-family:Arial">[<wbr>\f\n\r\t\v]<wbr></wbr></wbr></span><span style="font-family:宋体">等效。</span></wbr>
\S
匹配任何非空白字符。与<wbr><span style="font-family:Arial">[^<wbr>\f\n\r\t\v]<wbr></wbr></wbr></span><span style="font-family:宋体">等效。</span></wbr>
\t
制表符匹配。与<wbr><span style="font-family:Arial">\x09<wbr></wbr></span><span style="font-family:宋体">和<wbr></wbr></span><span style="font-family:Arial">\cI<wbr></wbr></span><span style="font-family:宋体">等效。</span></wbr>
\v
垂直制表符匹配。与<wbr><span style="font-family:Arial">\x0b<wbr></wbr></span><span style="font-family:宋体">和<wbr></wbr></span><span style="font-family:Arial">\cK<wbr></wbr></span><span style="font-family:宋体">等效。</span></wbr>
\w
匹配任何字类字符,包括下划线。与“[A-Za-z0-9_]”等效。
\W
与任何非单词字符匹配。与“[^A-Za-z0-9_]”等效。
\xn
匹配<wbr><span style="font-family:Arial">n</span><span style="font-family:宋体">,此处的<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">是一个十六进制转义码。十六进制转义码必须正好是两位数长。例如,</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\x41</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">匹配</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">A</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">。</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\x041</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">与</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">\x04</span><span style="font-family:Times New Roman">”</span><span style="font-family:Arial">&</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">1</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">等效。允许在正则表达式中使用<wbr></wbr></span><span style="font-family:Arial">ASCII<wbr></wbr></span><span style="font-family:宋体">代码。</span></wbr>
\num
匹配<wbr><span style="font-family:Arial">num</span><span style="font-family:宋体">,此处的<wbr></wbr></span><span style="font-family:Arial">num<wbr></wbr></span><span style="font-family:宋体">是一个正整数。到捕获匹配的反向引用。例如,</span><span style="font-family:Times New Roman">“</span><span style="font-family:Arial">(.)\1</span><span style="font-family:Times New Roman">”</span><span style="font-family:宋体">匹配两个连续的相同字符。</span></wbr>
\n
标识一个八进制转义码或反向引用。如果<wbr><span style="font-family:Arial">\n<wbr></wbr></span><span style="font-family:宋体">前面至少有<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">个捕获子表达式,那么<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">是反向引用。否则,如果<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">是八进制数<wbr></wbr></span><span style="font-family:Arial">(0-7)</span><span style="font-family:宋体">,那么<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">是八进制转义码。</span></wbr>
\nm
标识一个八进制转义码或反向引用。如果<wbr><span style="font-family:Arial">\nm<wbr></wbr></span><span style="font-family:宋体">前面至少有<wbr></wbr></span><span style="font-family:Arial">nm<wbr></wbr></span><span style="font-family:宋体">个捕获子表达式,那么<wbr></wbr></span><span style="font-family:Arial">nm<wbr></wbr></span><span style="font-family:宋体">是反向引用。如果<wbr></wbr></span><span style="font-family:Arial">\nm<wbr></wbr></span><span style="font-family:宋体">前面至少有<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">个捕获,则<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">是反向引用,后面跟有字符<wbr></wbr></span><span style="font-family:Arial">m</span><span style="font-family:宋体">。如果两种前面的情况都不存在,则<wbr></wbr></span><span style="font-family:Arial">\nm<wbr></wbr></span><span style="font-family:宋体">匹配八进制值<wbr></wbr></span><span style="font-family:Arial">nm</span><span style="font-family:宋体">,其中<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">和<wbr></wbr></span><span style="font-family:Arial">m<wbr></wbr></span><span style="font-family:宋体">是八进制数字<wbr></wbr></span><span style="font-family:Arial">(0-7)</span><span style="font-family:宋体">。</span></wbr>
\nml
当<wbr><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">是八进制数<wbr></wbr></span><span style="font-family:Arial">(0-3)</span><span style="font-family:宋体">,</span><span style="font-family:Arial">m<wbr></wbr></span><span style="font-family:宋体">和<wbr></wbr></span><span style="font-family:Arial">l<wbr></wbr></span><span style="font-family:宋体">是八进制数<wbr></wbr></span><span style="font-family:Arial">(0-7)<wbr></wbr></span><span style="font-family:宋体">时,匹配八进制转义码<wbr></wbr></span><span style="font-family:Arial">nml</span><span style="font-family:宋体">。</span></wbr>
\un
匹配<wbr><span style="font-family:Arial">n</span><span style="font-family:宋体">,其中<wbr></wbr></span><span style="font-family:Arial">n<wbr></wbr></span><span style="font-family:宋体">是以四位十六进制数表示的<wbr></wbr></span><span style="font-family:Arial">Unicode<wbr></wbr></span><span style="font-family:宋体">字符。例如,</span><span style="font-family:Arial">\u00A9<wbr></wbr></span><span style="font-family:宋体">匹配版权符号<wbr></wbr></span><span style="font-family:Arial">(?)</span><span style="font-family:宋体">。</span></wbr>
相关推荐
1. **正则表达式基本概念** - 元字符:如`.`, `*`, `+`, `?`, `{}`, `[]`, `\`, `^`, `$`等,它们具有特殊含义,在正则表达式中用来定义模式。 - 字符类:例如`[abc]`,匹配其中任意一个字符。 - 量词:`*`, `+`,...
1. 创建正则表达式:定义你要匹配的模式,例如`^[\d]{3}-[\d]{4}$`用于匹配美国电话号码格式。 2. 编译模式:将正则表达式编译成一个可以执行的对象,以提高后续匹配的速度。 3. 执行匹配:使用编译后的对象在目标...
在Qt框架中,正则表达式(Regular Expression)是一种强大的文本处理工具,它允许程序员以结构化的方式匹配、查找、替换或验证字符串。本项目针对Qt的lineEdit组件,通过正则表达式实现了输入限制功能,使得lineEdit...
1. JavaScript正则表达式在线测试工具:http://tools.jb51.net/regex/javascript 2. 正则表达式在线生成工具:http://tools.jb51.net/regex/create_reg 这些工具可以帮助开发者快速测试和生成正则表达式,从而提高...
1. **匹配**:检查输入字符串是否符合特定的正则表达式模式。 2. **查找**:在字符串中查找符合正则表达式的部分。 3. **替换**:用新的字符串替换匹配到的正则表达式部分。 4. **分割**:根据正则表达式将字符串...
在标准C++库中,虽然没有内置的正则表达式支持,但通过包含如 Boost 或 TR1(Technical Report 1)扩展,或者在较新的C++11及更高版本中使用库,开发者可以实现正则表达式功能。然而,对于MFC开发者来说,使用MFC内...
1. **语法兼容性**:如描述所述,PCRE库的正则表达式语法与Perl语言高度兼容,这意味着开发者可以利用Perl中广泛使用的正则表达式语法,如贪婪和非贪婪量词、分支选择、反向引用等。 2. **Unicode支持**:PCRE库...
本书自第1 版开始着力于教会读者“以正则表达式来思考”,来让读者真正“精通”正则表达式。该版对PHP的相关内容、Java1.5和Java1.6的新特性作了可观的扩充讲解。任何有机会使用正则表达式的读者都会从中获益匪浅。
1. **C#中的正则表达式基础** - `System.Text.RegularExpressions`命名空间:C#中的正则表达式操作主要基于这个命名空间下的类和方法。 - `Regex`类:这是处理正则表达式的核心类,提供了多种与正则相关的静态方法...
"正则表达式必知必会" 正则表达式是一种强大的文本处理工具,广泛应用于各个领域。下面是对正则表达式的详细解释: 正则表达式的用途 正则表达式主要用于处理文本,提供了两大主要功能:查找和替换。查找功能允许...
正则表达式验证工具 V1.0 本软件主要用于检测正则表达式是否正确。 运行环境:本软件为绿色软件,无需安装,但需要Microsoft .NET Framework 4 支持,如果没有请前去下载(下载路径:...
1. **正则表达式基础** - **元字符**:如`.`表示任意字符,`^`表示行首,`$`表示行尾,`\d`代表数字,`\w`代表字母或数字,`\s`代表空白字符。 - **量词**:如`*`表示前面的元素零次或多次,`+`表示一次或多次,`?...
正则表达式作为一种文本处理工具,在计算机编程和数据处理领域中扮演着极其重要的角色。它不仅适用于几乎所有编程语言和计算机平台,而且能够执行复杂的文本搜索、匹配、替换和提取操作。正则表达式的核心是通过定义...
1. **向导式界面**:对于不熟悉正则语法的用户,该工具可能提供了一个友好的向导,逐步引导用户构建复杂的正则表达式。 2. **实时预览**:用户输入正则表达式后,工具会立即在文本框中显示匹配结果,有助于快速调试...
1. **模式构建**:提供图形化界面或者交互式输入,帮助用户构建复杂的正则表达式模式。用户可以通过选择预定义的元字符、量词、字符类等元素,轻松构建出符合需求的模式。 2. **实时预览**:在构建过程中,工具会...
1. **元字符**:正则表达式中的特殊字符,用于指定模式。例如,“.”可以匹配任何单个字符(除了换行符),而“*”表示匹配前面的字符零次或多次。 2. **量词**:用来指定模式出现的次数。例如,“a{3}”表示匹配...
正则表达式1介绍教程 正则表达式(Regular Expression,简称regex)是处理字符串的强大工具,用于在文本中匹配、查找、替换或提取特定模式的字符串。它由一系列字符和特殊符号组成,用于定义一个搜索模式。在编程...
Java正则表达式匹配工具是IT领域中一种强大的文本处理工具,它利用正则表达式(Regular Expression)的规则来查找、替换或者提取文本中的特定模式。正则表达式是一种特殊的字符序列,能够帮助程序员或者用户高效地...
在IT行业中,正则表达式(Regular Expression)是一种强大的文本处理工具,用于匹配、查找、替换等操作。C++作为一种通用编程语言,虽然标准库中没有内置正则表达式支持,但通过第三方库如Boost,我们可以很方便地在...