日期:2014-05-20  浏览次数:20535 次

匹配字符串 "XXX" 正则表达式怎么写
假设现在有一字符串
"tony,candy,gay ",
现在想知道该字符串是否存在字符串 "candy ",   我想用正则表达式会比较容易实现吧,   请问正式表达要怎么写?
谢谢解答!

------解决方案--------------------
string s = "tony,candy,gay ";
int i=s.IndoxOf( "candy ");
if (i> 0)//存在
{}

------解决方案--------------------
var a = "tosny,casndy,ss ";
var r = /(.*)(casndy).*$/;
if(!r.test(a))
{
alert( "false ");
}
else
{
alert( "true ");
}
------解决方案--------------------
判断是否包含子串时用IndexOf(),不包含返回值为-1,所以要和-1进行比较,首选取这种方法
string yourStr = "tony,candy,gay ";
int i=yourStr.IndexOf( "candy ");
if (i> -1)
{
//存在
}

如果是用正则,楼主的正则里“{1}”是没有必要的
------解决方案--------------------
http://www.unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm
http://www.why100000.com/_articles/show_a_article.asp?autoid=30&tab=tabjava