duannan4486
duannan4486
2013-03-15 07:54
浏览 73
已采纳

正则表达式,在模式之前找到10个字符

I'm not good at regex so I need some help.

I'm trying to find how to get for example 10 characters before the pattern and 10 characters after. like:

dskjgh kdsfhgl:ks;dhfl\kgh.sldfgdfgsd<pattern1.fso[wpe;x,/c,zasdfsdg


(10 chars before pattern1) var1 = "ldfgdfgsd<"
(10 chars after pattern1)  var2 = ".fso[wpe;x"
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsndm82062
    dsndm82062 2013-03-15 08:17
    已采纳
    $str = "dskjgh kdsfhgl:ks;dhfl\\kgh.sldfgdfgsd<pattern1.fso[wpe;x,/c,zasdfsd";
    preg_match( "/(.{10})(pattern1)(.{10})/", $str, $matches);
    
    $var1 = $matches[1];
    echo $var1;
    
    $var2 = $matches[3];
    echo $var2;
    
    点赞 评论
  • dpxnh80648
    dpxnh80648 2013-03-15 07:59

    This should do the trick:

    var str = "lasdjfl askjdfl askjdf kljlajpattern1lakjsdflajsdlfkj";
    var match = str.match(/(.{10})pattern1(.{10})/i);
    var text1 = match[1];
    var text2 = match[2];
    alert(text1);
    alert(text2);
    

    Results:

    text1 = jdf kljlaj
    
    text2 = lakjsdflaj
    
    点赞 评论

相关推荐