コード例 #1
0
def test_number():
    int_pattern = r.seq(r.opt(r.alt(r.quote("+"), r.quote("-"))),
                        r.repeat(1, None, r.char_from("0123456789")))
    num_pattern = r.seq(int_pattern,
                        r.opt(r.seq(r.quote("."),
                                    r.opt(int_pattern))))
    number = re.compile(num_pattern)

    assert not number.fullmatch("foo")
    assert not number.fullmatch("+")
    assert number.fullmatch("0")
    assert number.fullmatch("-42")
    assert number.fullmatch("3.")
    assert number.fullmatch("3.14")
コード例 #2
0
def test_unlimited_repetition():
    pattern = r.repeat(2, None, r.alt(r.quote("cat"), r.quote("dog")))
    assert not re.fullmatch(pattern, 'cat')
    assert re.fullmatch(pattern, 'catdog')
    assert re.fullmatch(pattern, 'dogcatcat')
    assert re.fullmatch(pattern, 'dogcatcatdogdogcat')
コード例 #3
0
def test_repetition_max_less_than_min_is_ignored():
    assert r.repeat(2, 1, "foo") == "(foofoo)"
コード例 #4
0
def test_repetition_min_max():
    assert r.repeat(2, 4, "foo") == "(foofoo(foo|)(foo|))"
コード例 #5
0
def test_repetition_no_max():
    assert r.repeat(2, None, r.quote("foo")) == "((foo)(foo)(foo)*)"
コード例 #6
0
def test_repetition_min_same_as_max():
    assert r.repeat(3, 3, "foo") == "(foofoofoo)"