View Problem

Check if a string contains a match to a regular expression

Display "ok" if "abc 123 @#$" matches /\d+/
DiskEdit
clojure
(if (re-find #"\d+" "abc 123 @#$")
(println "ok"))
ExpandDiskEdit
cpp C++/CLI .NET 2.0
if (Regex::IsMatch("abc 123 @#$", "\\d+")) Console::WriteLine("ok");
DiskEdit
csharp
if(System.Text.RegularExpressions.Regex.IsMatch("abc 123 @#$",@"\d+")){
Console.WriteLine("ok");
}
ExpandDiskEdit
erlang
% Erlang uses 'egrep'-compatible regular expressions, so shortcuts like '\d' not supported
String = "abc 123 @#$", Regexp = "[0-9]+",
is_match(String, Regexp) andalso (begin io:format("ok~n"), true end).
DiskEdit
erlang 12B3+
case re:run("abc 123 @#$", "\\d+") of {match, _} -> ok end.
ExpandDiskEdit
fantom
m := Regex<|\d+|>.matcher("abc 123 @#\$")
if (m.find)
echo("ok")
ExpandDiskEdit
fsharp
if (Regex.IsMatch("abc 123 @#$", "\\d+")) then printfn "ok"
DiskEdit
groovy
if ('abc 123 @#$' =~ /\d+/) println 'ok'
DiskEdit
groovy 1.6.1+
if ('abc 123 @#$'.find(/\d+/)) println 'ok'
ExpandDiskEdit
java
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println("ok");
}
DiskEdit
ocaml
#load "str.cma" ;;

let re = Str.regexp "[0-9]+" in
try let _ = Str.search_forward re "abc 123 @#$" 0 in
print_string "ok"
with _ -> ()
DiskEdit
perl
print "ok" if ("abc 123 @#\$" =~ m/\d+/)
ExpandDiskEdit
php
if (preg_match("/\d+/", "abc 123 @#$"))
echo "ok";
DiskEdit
python
found = re.search(r'\d+', 'abc 123 @#$')
if found:
print 'ok'
ExpandDiskEdit
ruby
puts "ok" if (text=~/\d+/)
ExpandDiskEdit
scala
if (Pattern.compile("\\d+").matcher("abc 123 @#$").find) println("ok")

Submit a new solution for clojure, cpp, csharp, erlang ...
There is 1 other solution in haskell