search

Home  >  Q&A  >  body text

php - Regular match 5 consecutive 1212121212 or 2121212121, numbers can be reused

For example string 1212121212121212121212

It needs to match 5 consecutive 12s or 5 consecutive 21s to be considered qualified

The number of matches should be 13, as shown in the figure below, the red lines represent matches.

漂亮男人漂亮男人2704 days ago1114

reply all(6)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-05 10:00:15

    Consider extraction without occupancy and use look-around to extract subgroups: (?=.*?((12|21)2{4}))

    Demo link: http://regex.zjmainstay.cn/r/...

    reply
    0
  • 大家讲道理

    大家讲道理2017-07-05 10:00:15

    >>> import re
    >>> ss='1212121212121212121212'
    >>> re.findall(r'(?=((12|21){4}))',ss)
    [('1212121212', '12'), ('2121212121', '21'), ('1212121212', '12'), ('2121212121', '21'), ('1212121212', '12'), ('2121212121', '21'), ('1212121212', '12'), ('2121212121', '21'), ('1212121212', '12'), ('2121212121', '21'), ('1212121212', '12'), ('2121212121', '21'), ('1212121212', '12')]

    js's Re engine is a bit rough and needs to loop back to submatches.

    var str="1212121212121212121212";
    var pattern=/(?=((12|21){4}))/g;
    while(m = pattern.exec(str)){
        console.log(m[1])
        pattern.lastIndex++ //由于没有消耗字符,js的Re引擎不会递增索引。
    }
    

    reply
    0
  • typecho

    typecho2017-07-05 10:00:15

    The question is unclear, I can only guess. .

    (([0-9]){2})+

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-05 10:00:15

    /((12){5})|((21){5})/.test(str)

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-05 10:00:15

    var pattern=/(?:(1)(?=(?:21){4}2))|(?:(2)(?=(?:12){4}1))/g;
    var str="1212121212121212121212";
    console.log(str.match(pattern));

    reply
    0
  • 高洛峰

    高洛峰2017-07-05 10:00:15

    /(w+)1{4}/

    reply
    0
  • Cancelreply