一些好用的正则表达式

前端之家收集整理的这篇文章主要介绍了一些好用的正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参考

http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html



1,判断字符串中是否含有中文

using System.Text.RegularExpressions;

[csharp] view plain copy
  1. if(Regex.IsMatch(gst.Customer.EnglishName,@"[\u4e00-\u9fa5]+"))
  2. {
  3. ShowMessage("客人的英文名含有汉字");
  4. return;
  5. }


2,截取中间部分字符串

//string s = Regex.Replace(" abra ",@"^\s*(.*?)\s*$","$1");
//return s; // 返回abra(它前后的空格被去掉了)


Regex.Replace("abcdef009gasdf879653","^.*09(.*?)879.*$","$1") 将会返回gasdf


Regex.Replace(ctx,".*<div class=\"sl_top\"> <b><a name=\"top\"></a>首尾班车时刻表</b>(.*?)</div> <p class=\"notice\">注:.*","$1")


3,截取字符串中的数字

string sTest = "3239*aa";
string sResult = Regex.Replace(sTest,@"[^\d]",""); // 返回3239


4, 分组匹配

\b\w+(?=ing\b)

I'm singing while you're dancing.

能匹配到2个结果: sing 和 danc


\b\w*q[^u]\w*\b

Iraq fighting how

匹配到1个结果:Iraq fighting


原始字符串->constraint "FK_q_contract_q_1quote". database "QBaseDemoS1",table "dbo.q_contract",column 'quote_id'.

正则表达式->^.*constraint "(.*?)". database "(.*?)",table "(.*?)",column '(.*?)'..*$


原始字符串->The DELETE statement conflicted with the REFERENCE constraint "FK_q_contract_q_1quote". The conflict occurred in database "QBaseDemoS1",column 'quote_id'.

正则表达式->^.*REFERENCE constraint "(.*?)".*database "(.*?)",column '(.*?)'..*$


private String Extension2(String sContent)
{
Regex r = new Regex(@"^.*REFERENCE constraint ""(.*?)"".*database ""(.*?)"",table ""(.*?)"",column '(.*?)'..*$",RegexOptions.Compiled);
return r.Match(sContent).Result("${1}|${2}|${3}|${4}"); //返回FK_q_contract_q_1quote|QBaseDemoS1|dbo.q_contract|quote_id
}


// 测试内容http://www.yahoo.com.cn:8080/index.html

private String Extension1(String sContent)
{

Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>\d+)?/",RegexOptions.Compiled);
return r.Match(sContent).Result("${proto}|${port}"); //返回http|8080

}



5, 4个样例

LIFEPRINT20130928.ZIP

LIFEPRINT_ASM_ENCOUNTERS20130928_2013-10-25_0534.txt

WMCOSMOS20140115_RXCHF70CL.TXT

分别对应 正则表达式:

^\w+\d{8}\.zip$
^.*_\d{4}-\d{2}-\d{2}_\d{4}\.txt$

^WMCOSMOS\d{8}_RXCHF70CL.TXT$

Regex.IsMatch("FileB_837I_PHC_5010_1191058614_PHC_TX_837I_03272014.xml","^FileB_837(.*?)xml$",RegexOptions.IgnoreCase)

用Directory.GetFile 则 需这样:string[] arrDoneFile = System.IO.Directory.GetFiles(sNewDone,"FileB_*.xml");


6,检查字符串A中是否包含字符串B

A= sPageInfo

B= "<div id=\"dvError\" style=\"color:blue;font-size:30px;\"><br />系统忙,请稍侯重试!</div>" --> B

Regex.Matches(A,B).Count >=1 表示 A包含B

eg: Regex.Matches(sPageInfo,"<div id=\"dvError\" style=\"color:blue;font-size:30px;\"><br />系统忙,请稍侯重试!</div>").Count


7,正则移除部分字符串

String sTest = "你好吗,我是小王!(备注1:我们是同事)(备注2:我们还是邻居)【备注3:我们都是开发人员】[备注4:都搞.net]都擅长Oracle操作";
sTest = Regex.Replace(sTest,@" . ","");//结果:你好吗,我是小王!(备注2:我们还是邻居)【备注3:我们都是开发人员】[备注4:都搞.net]都擅长Oracle操作
sTest = Regex.Replace(sTest,@"(.*)","");//结果:你好吗,我是小王!【备注3:我们都是开发人员】[备注4:都搞.net]都擅长Oracle操作
sTest = Regex.Replace(sTest,@"【.*】","");//结果:你好吗,我是小王![备注4:都搞.net]都擅长Oracle操作
sTest = Regex.Replace(sTest,@"
@H_404_228@ .
","");//结果:你好吗,我是小王!都擅长Oracle操作
sTest = Regex.Replace(sTest,@"都擅长.*","");//结果:你好吗,我是小王!



1,匹配所有标签

regex:
\<.[^<>]*\>

source:
<external_network_location_id>20130401_TXNONC100FFS3101TAUSNPN1733590048828A_0048828</external_network_location_id>

result:
<external_network_location_id>
</external_network_location_id>

2,匹配指定标签 eg:匹配指定的div标签

regex:
\<\bdiv.*\<\/div\b\>

source:
<div>23dd</div>
<div1>23dd</div1>
<div>23dd33ff</div>

result:
<div>23dd</div>
<div>23dd33ff</div>

3,匹配某种特定格式的字符串

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
regex:
>.[^<>]+<

source:
<li><a href="http://sports.sina.com.cn"target="_blank">新浪</a></li>
<li><a href="http://news.yule.com.cn"target="_blank">中国娱乐网</a></li>
<li><a href="http://pzh.scol.com.cn"target="_blank">四川在线</a></li>
<li><a href="http://news.0755car.com"target="_blank">车神榜</a></li>

result:
>新浪<
>中国娱乐网<
>四川在线<
>车神榜<

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

regex 2:
<li><a href="(.*?)"target="_blank">(.*?)</a></li>

source:
<li><a href="http://sports.sina.com.cn"target="_blank">新浪</a></li>
<li><a href="http://news.yule.com.cn"target="_blank">中国娱乐网</a></li>
<li><a href="http://pzh.scol.com.cn"target="_blank">四川在线</a></li>
<li><a href="http://news.0755car.com"target="_blank">车神榜</a></li>

result:
http://sports.sina.com.cn新浪
http://news.yule.com.cn中国娱乐网
http://pzh.scol.com.cn四川在线
http://news.0755car.com车神榜

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

regex:
http://([^\s]+)".+?span.+?\[(.+?)\].+?>(.+?)<

source:
<li><a href="http://www.wea.com/blog/a.html" title="怎样在百度空间添加友情链接"><span class="article-date">[2014/11/13]</span>怎样在百链接</a></li>
<li><a href="http://www.a.com/blog/b.html2" title="怎样在百度空间添加友情链接2"><span class="article-date">[2014/11/12]</span>怎样在百度链接2</a></li>

result:
http://www.wea.com/blog/a.html2014/11/13怎样在百链接
http://www.a.com/blog/b.html22014/11/12怎样在百度链接2

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

regex:
<external_network_location_id>(.*?)</external_network_location_id>

source:
<external_network_location_id>20130401_TXNONC100FFS3101TAUSNPN1733590048828A_0048828</external_network_location_id>
<external_network_location_id>abcd1234004488877</external_network_location_id>

result:
20130401_TXNONC100FFS3101TAUSNPN1733590048828A_0048828
abcd1234004488877

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

regex:
<requserid>([^<]+)</requserid>

source:
<Request><Action>getuser</Action><UserLogin></UserLogin><Password></Password><Signature></Signature><VerifyText></VerifyText><requserid>535</requserid><requserid>5335</requserid></Request>

result:
535
5335

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

5,提取所有标签中的内容

regex 1:
<.+?>(.+?)<.+?>

regex 2:
(?is)(?<=>)[^<>]+(?=<)

source:
<span style=''>内容1</span><img src=".."/>内容2<p><input .../>内容3</p><p>内容4</p><b>内容5</b><i>内容6</i>

result:
内容1
内容2
内容3
内容4
内容5
内容6

6,提取所有 img标签中的属性值 (其它标签可以借鉴)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
regex:
(?is)<img\s*((?<key>[^=]+)="(?<value>[^"]+)")+?\s*/?>

source:
<img src="acbdd"/><img src="33ff"/><img src="gggggeeee"/><a>33333</a>

result:
key=srcvalue=acbdd
key=srcvalue=33ff
key=src value=gggggeeee

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

regex 1:
(?is)<img\s*((?<key>[^=]+)=(["'])(?<value>[^'"]+)\2)+?\s*/?>([^<>]*?</img>)?
regex 2:
(?is)<img\s+((?<key>[^=]+)=(["']?)(?<value>[^'"]+)\2\s*)+?\s*/?>([^<>]*?</img>)?

source:
<img src="acbdd"/><img src="33ff"/><img src="gggggeeee"/><img src="bb"></img><a>33333</a>

result:
key=srcvalue=acbdd
key=srcvalue=33ff
key=src value=gggggeeee
key=srcvalue=bb

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

(?<=^<external_provider_group_id>).*(?=</external_provider_group_id>)
可取第一个 23dd0078243d4323
贪婪 匹配
<external_provider_group_id>23dd0078243d4323</external_provider_group_id>

可取第一个 23dd0078243d4323
贪婪 匹配
(?<=^<external_provider_group_id>).*0078243.*(?=</external_provider_group_id>)


<[A-Za-z_-]+>\w+0078243\w+</[A-Za-z_-]+>
可取以下4项
\w+ 表示 取1个或多个
<external_provider_group_id>23dd0078243d4323</external_provider_group_id>
<a>dd0078243dsd</a>
<b_b>dd0078243dsd33</b_b>
<c-c>dd0078243dsd44</c-c>

<[A-Za-z_-]+>\w{0,}0078243\w{0,}</[A-Za-z_-]+>
可取以下6项
\w{0,} 表示取0个或多个
<external_provider_group_id>23dd0078243d4323</external_provider_group_id>
<external_provider_group_id>442232323</external_provider_group_id>
<external_provider_group_id>23dd0078243d432344</external_provider_group_id>
<a>dd0078243dsd</a>
<b_b>dd0078243dsd33</b_b>
<c-c>dd0078243dsd44</c-c>
<d-d>0078243</d-d>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

其它:

(?<=^<[A-Za-z_-]+>).*(?=</[A-Za-z_-]+>) 只能取第一个 .*(?<=<\w+>.*</\w+>)*

<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 原文链接:https://www.f2er.com/regex/359796.html

猜你在找的正则表达式相关文章