使用正则表达式搜索字符串从指定位置开始到指定位置间的特定字符串例如搜索"aceeawerrt32awerrww46"中在第2个字符间到第12个字符间的"werr"字符串用正则表达式该如何写

解决方案 »

  1.   

    不是很明白楼主的意思,你是想查询指定子字符串里是否有特定的字符串吗,那没必要用正则的string yourStr = "aceeawerrt32awerrww46";
    int startIndex = 2;
    int endIndex = 12;
    int strIndex = yourStr.Substring(startIndex-1,endIndex-startIndex+1).IndexOf("werr");
    if(strIndex>-1)
    {
        //包含
    }
    else
    {
        //不包含
    }这里只是为了结构清晰和具有灵活性这样写的
      

  2.   

    这个-_-#,这样
    string yourStr = "aceeawerrt32awerrww46";
    int startIndex = 2;
    int endIndex = 12;
    string resultStr = yourStr.Substring(startIndex-1,endIndex-startIndex+1);
    if(Regex.IsMatch(resultStr,@"werr"))
    {
        //包含
    }
    else
    {
        //不包含
    }
      

  3.   

    string str="aceeawerrt32werrww46";
    int iStart=1;
    int iConut=12;
    int iEnt=str.Length- iStart-iConut;
    Regex reg;
    if(iEnt>=0)
    reg=new Regex("^.{"+iStart+"}.*werr.*.{"+iEnt+"}$");//如果不加判断 只使用此正则长度 大于13,会false
    else
    {
    reg=new Regex("^.{"+iStart+"}.*werr.*");//这样是要匹配
    }
    bool b=reg.IsMatch(str);