View Problem

Check if a string matches with groups

Display "two" if "one two three" matches /one (.*) three/
DiskEdit
ruby
puts $1 if "one two three"=~/^one (.*) three$/
ExpandDiskEdit
cpp C++/CLI .NET 2.0
Match^ match = Regex::Match("one two three", "one (.*) three");
if (match->Success) Console::WriteLine("{0}", match->Groups[1]->Captures[0]);
ExpandDiskEdit
cpp
cmatch what;
if (regex_match("one two three", what, regex("one (.*) three")))
cout << what[1] << endl;
ExpandDiskEdit
fantom
m := Regex<|one (.*) three|>.matcher("one two three")
if (m.matches)
echo("${m.group(1)}")
DiskEdit
clojure
(if-let [groups (re-matches #"one (.*) three" "one two three")]
(println (second groups)))
ExpandDiskEdit
fsharp
let regmatch = (Regex.Match("one two three", "one (.*) three"))
if regmatch.Success then (printfn "%s" (regmatch.Groups.[1].Captures.[0].ToString()))
ExpandDiskEdit
go
re, _ := regexp.Compile("one (.*) three")
groups := re.FindStringSubmatch("one two three")
if len(groups) > 0 {
fmt.Println(groups[1])
}
DiskEdit
groovy
matcher = ("one two three" =~ /one (.*) three/)
if (matcher) println matcher[0][1]
DiskEdit
groovy 1.6.1+
match = "one two three".find("one (.*) three") { it[1] }
if (match) println match

Submit a new solution for ruby, cpp, fantom, clojure ...
There are 10 other solutions in additional languages (csharp, erlang, haskell, java ...)