View Problem

Replace all regex matches in a string with a static string

Transform "She sells sea shells" into "She X X shells" by replacing /se\w+/ with "X"
ExpandDiskEdit
ruby
replaced = text.gsub(/se\w+/,"X")
ExpandDiskEdit
java
String replaced = text.replaceAll("se\\w+", "X");
DiskEdit
perl
$text = "She sells sea shells";
$text =~ s/se\w+/X/g;
ExpandDiskEdit
groovy
replaced = text.replaceAll(/se\w+/,"X")
ExpandDiskEdit
scala
val replaced = "She sells sea shells".replaceAll("se\\w+", "X")
DiskEdit
python
transformed = re.sub(r'se\w+', 'X', 'She sells sea shells')
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ Replaced = (gcnew Regex("se\\w+"))->Replace("She sells sea shells", "X");
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ Replaced = Regex::Replace("She sells sea shells", "se\\w+", "X");
ExpandDiskEdit
fsharp
let replaced = ((new Regex("se\\w+")).Replace("She sells sea shells", "X"))
printfn "%s" replaced
ExpandDiskEdit
erlang
% Erlang uses 'egrep'-compatible regular expressions, so shortcuts like '\w' not supported
{ok, Replaced, _} = regexp:gsub("She sells sea shells", "se[A-Za-z0-9_]+", "X"),
DiskEdit
erlang 12B3+
re:replace("She sells sea shells", "se\\w+", "X", [global, {return, list}]).
DiskEdit
ocaml
let s = "She sells sea shells" in
Str.global_replace (Str.regexp "se[^ \\t\\n]*") "X" s
DiskEdit
csharp
using System.Text.RegularExpressions;

class SolutionXX
{
static void Main()
{
string text = "She sells sea shells";
string result = Regex.Replace(text, @"se\w+", "X");
}
}
ExpandDiskEdit
php
echo preg_replace('/se\w+/', 'X', 'She sells sea shells');
DiskEdit
clojure
(.replaceAll (re-matcher #"se\w+" "She sells sea shells") "X")
ExpandDiskEdit
fantom
replaced := Regex<|se\w+|>.split("She sells sea shells").join("X")

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