如何匹配序列组?

时间:2013-08-04 12:20:46

标签: regex

说,给定的字符串是abcwhateverdefwhatever34567whatever012如何匹配那些像匹配abc, def, 34567,012那样顺序的组?

我现在的正则表达式是(.)\1{2,}但是它匹配相同的字符但不是按顺序匹配

2 个答案:

答案 0 :(得分:1)

如果你还在寻找PHP代码。

function getSequence($str) {
    $prev = 0; $next = 0; $length = strlen($str);

    $temp = "";
    for($i = 0; $i < $length; $i++) {
        $next = ord($str[$i]);
        if ($next == $prev + 1) {
            $temp .= $str[$i];
        } else {
            if (strlen($temp) > 1) $result[] = $temp;
            $temp = $str[$i];
        }
        $prev = $next;
    }

    if (strlen($temp) > 1) $result[] = $temp;

    return $result;
}

$str = "abcwhateverdefwhatever34567whatever012";
print_r(getSequence($str));

答案 1 :(得分:1)

这是解决正则表达式问题的解决方案。虽然效率不高,但我不推荐它。

from re import findall, X

text = "abcwhateverdefwhatever34567whatever012"

reg = r"""
(?:
(?:0(?=1))|
(?:(?<=0)1)|(?:1(?=2))|
(?:(?<=1)2)|(?:2(?=3))|
(?:(?<=2)3)|(?:3(?=4))|
(?:(?<=3)4)|(?:4(?=5))|
(?:(?<=4)5)|(?:5(?=6))|
(?:(?<=5)6)|(?:6(?=7))|
(?:(?<=6)7)|(?:7(?=8))|
(?:(?<=7)8)|(?:8(?=9))|
(?:(?<=8)9)|

(?:a(?=b))|
(?:(?<=a)b)|(?:b(?=c))|
(?:(?<=b)c)|(?:c(?=d))|
(?:(?<=c)d)|(?:d(?=e))|
(?:(?<=d)e)|(?:e(?=f))|
(?:(?<=e)f)
){1,}
"""

print findall(reg, text, X)

结果是:

['abc', 'def', '34567', '012']

正如您所看到的,我只在字母表中添加了数字和前6个字母。如何继续应该是相当明显的。