Skip to content

Instantly share code, notes, and snippets.

@joseoliv joseoliv/Regex.cyan
Created Jun 15, 2018

Embed
What would you like to do?
Metaobject regex
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.