View Problem

Check if a string matches a regular expression

Display "ok" if "Hello" matches /[A-Z][a-z]+/
DiskEdit
ruby
puts "ok" if ("Hello"=~/^[A-Z][a-z]+$/)
ExpandDiskEdit
java
if ("Hello".matches("[A-Z][a-z]+")) {
System.out.println("ok");
}
DiskEdit
perl
print 'ok' if ('Hello' =~ /[A-Z][a-z]+/);
DiskEdit
groovy
if ("Hello" =~ /[A-Z][a-z]+/) println 'ok'
DiskEdit
groovy 1.6.1+
if ("Hello".find(/[A-Z][a-z]+/)) println 'ok'
DiskEdit
groovy 1.6.1+
// with precompiled regex
def regex = ~/[A-Z][a-z]+/
if ("Hello".find(regex)) println 'ok'
DiskEdit
groovy 1.6.1+
// with precompiled regex
def regex = ~/[A-Z][a-z]+/
if ("Hello".matches(regex)) println 'ok'
DiskEdit
groovy
if ("Hello".matches("[A-Z][a-z]+")) println 'ok'
ExpandDiskEdit
scala
if ("Hello".matches("[A-Z][a-z]+")) println("ok")
DiskEdit
python
found = re.match(r'[A-Z][a-z]+', 'Hello')
if found:
print 'ok'
ExpandDiskEdit
cpp C++/CLI .NET 2.0
if ((gcnew Regex("[A-Z][a-z]+"))->IsMatch("Hello")) Console::WriteLine("ok");
ExpandDiskEdit
cpp C++/CLI .NET 2.0
if (Regex::IsMatch("Hello", "[A-Z][a-z]+")) Console::WriteLine("ok");
ExpandDiskEdit
cpp C++/CLI .NET 2.0
Regex^ rx = gcnew Regex("[A-Z][a-z]+");
if (rx->IsMatch("Hello")) Console::WriteLine("ok");
ExpandDiskEdit
cpp
cmatch what;
if (regex_match("Hello", what, regex("[A-Z][a-z]+")))
cout << "ok" << endl;
ExpandDiskEdit
fsharp
if (Regex.IsMatch("Hello", "[A-Z][a-z]+")) then printfn "ok"
ExpandDiskEdit
erlang
String = "Hello", Regexp = "[A-Z][a-z]+",
is_match(String, Regexp) andalso (begin io:format("ok~n"), true end).
DiskEdit
erlang 12B3+
case re:run("Hello", "[A-Z][a-z]+") of {match, _} -> ok end.
DiskEdit
ocaml
if Str.string_match (Str.regexp "[A-Z][a-z]+") "Hello" 0
then print_string "ok";;
ExpandDiskEdit
csharp
if (Regex.IsMatch("Hello", "[A-Z][a-z]+"))
{
Console.WriteLine("ok");
}
ExpandDiskEdit
php <5.3
if(ereg('[A-Za-z]+', 'Hello')) {
echo "ok";
}
ExpandDiskEdit
php
if(preg_match('/[A-Za-z]+/', 'Hello')>0) {
echo "ok";
}
DiskEdit
haskell
import Text.Regex.Posix
main = if "Hello" =~ "[A-Z][a-z]+" then putStrLn "OK" else return ()
DiskEdit
clojure
(if (re-matches #"[A-Z][a-z]+" "Hello")
(println "ok"))
ExpandDiskEdit
fantom
if (Regex<|[A-Z][a-z]+|>.matches("Hello"))
echo("ok")
ExpandDiskEdit
go
result, _ := regexp.MatchString("[A-Z][a-z]+", "Hello")
if result {
fmt.Println("ok")
}

Submit a new solution for ruby, java, perl, groovy ...