Perl's and Rulz' regexp match operator is //, both default to match $_, both set the $1, $2, etc. and $& special variables.
Basic syntax:
"Hello World" =~ /World/; # matches (Perl)
/World/ "Hello World" # matches (Rulz)
Special variables and return:
$_= foobar; $n = /(foo)/; print "$_ $n $& $1" # foobar 1 foo foo
_ foobar /(foo)/ ^ $_ $# $& $1 # foobar 1 foo foo
Rulz' special variable $0 — common "return value" — emulates PHP's preg_match() function's $matches; in this case:
^ # (foo,foo)
More examples from Perlretut.
Basic conditional:
if ("Hello World" =~ /World/) {
print "It matches.\n";
}
else {
print "It doesn't match.\n";
}
Match sense reversed:
if ("Hello World" !~ /World/) {
print "It doesn't match.\n";
}
else {
print "It matches.\n";
}
Regexp in variable:
$greeting = "World";
if ("Hello World" =~ /$greeting/) {
print "It matches.\n";
}
else {
print "It doesn't match.\n";
}
Basic conditional:
/World/ "Hello World" ? ^"It matches.\n" ! ^"It doesn't match.\n"
Match sense reversed:
/World/ "Hello World" ! ^"It doesn't match.\n" ? ^"It matches.\n"
Regexp in variable:
= g "World" /$g/ "Hello World" ? ^"It matches.\n" ! ^"It doesn't match.\n"