text Metaobject正则表达式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Metaobject正则表达式相关的知识,希望对你有一定的参考价值。

package metaobjectTest


@doc{*
    usage: 
          String@regex(re)
    regex checks if the variable holds only strings that match the
    regular expression re.
*}
object Regex

    func run {
    
        
        var String@regex("a*b[A-Z]") ab;
        var String s;
        ab = "bB";   // ok
        ab = "aaaaabC";   // ok
        // ab = "a26"; // compile-time error
        
        var Boolean ok = true;
        s = "bB";
        {
            ab = s;   
        } catch: { (: ExceptionStr e :) ok = false };
        assert ok;

        ok = true;
        s = """aaaaabC""";
        {
            ab = s;
        } catch: { (: ExceptionStr e :) ok = false; };
        assert ok;
        
        
        ok = false;
        s = "abAZ";
        {
            ab = s; // fails at runtime
        } catch: { (: ExceptionStr e :) ok = true; };
        assert ok;
        
        ok = false;
        s = "aaaa";
        {
            ab = s; // fails at runtime
        } catch: { (: ExceptionStr e :) ok = true };
        assert ok;
        

end

以上是关于text Metaobject正则表达式的主要内容,如果未能解决你的问题,请参考以下文章

text Metaobject callUnaryMethods

text Metaobject callTestMethods

text Metaobject callRunMethods

text Metaobject类型

text Metaobject范围

text Metaobject annot