

catch{[api.loadModule 'pcreish']} && ((const msg = "pcre support is not enabled."), [print msg], return msg)

const RX = api.pcre

$print {
/**
   PCRE support is highly experimental and the API is not final.
*/
}

const S = api.string

0 || scope {

    var pat = "<[^>]+>(.*)</[^>]+>"
    var str = "<b>example: </b><div align=left>this is a test</div>"
    var rx = $RX pat // RX.flags.UNGREEDY
    $print 'rx =' rx rx.pattern
    assert pat === rx.pattern
    var x = $rx.exec str
    $print 'm =' x

    set rx = $RX {PERL} RX.flags.CASELESS
    assert RX.flags.CASELESS & rx.flags
    set x $rx.exec "this is not perl"
    assert 'perl' === x.0

}

0 || scope {
    var rx = $RX {(a|(z))(bc)}
    var x = $rx.exec "abc"
    $print 'm =' x
}

0 || scope {
    var rx = $RX {(abc)(x(yz)?)?}
    var x = $rx.exec "abc"
    $print 'm =' x
}

0 || scope {
    var rx = $RX {a((b)?c)} (RX.flags.CASELESS)
    var x = $rx.exec "abcACabc"
    //$print (x.0).1 // BUG: x.0.1 should work. t10n'd as ((x).(double 0.1))
    //$print x.0.(1) // BUG: t10n error: invalid floating point
    //assert 'abc' === (x.0).1
    $print 'm =' x
}


0 || scope {
    var pat = "(abc|def)+"
    var rx = $RX pat (RX.flags.CASELESS | RX.flags.JAVASCRIPT_COMPAT)
    //$print 'rx.flags =' rx.flags
    assert RX.flags.CASELESS & rx.flags
    //$print 'm =' rx rx.pattern rx.flags
    assert pat === rx.pattern
    assert [rx.test "abc"]
    assert [rx.test "zabcz"]
    assert ![rx.test "zz"]
    var str = "zzzabcabcdefzdefghi"
    var x = $rx.exec str;
    $print 'm =' x
    return
    assert 3 === [x.length]
    assert 'abcabcdef' === x.0
    assert 'def' === x.1
    assert 'b' === x.2
    assert 1 === [refcount x]
    set str = "...a.c..."
    set x = $rx.exec str
    $print x
    // very confused by this result
    //assert 1 === [x.length]
    //assert 'a.c' === x.0

    set rx undefined
    $print "done"
}

0 || scope {

    var pat = "(?P<abc>x+)+"
    var rx = $RX pat
    var str = "xxy xxxxz"
    var x = [rx.exec str]
    [print x x.names]

}


0 || scope {

    var pat = {\b(\d+)\.(\d+)\.(\d+)\.(\d+)\b}
    var rx = $RX pat RX.flags.UNGREEDY
    var str = "192.168.0.1"
    var x = [rx.exec str]
    [print x]
}

//$print RX.flags

return {done with script #9}
