Jump to content

Regular expressions: Difference between revisions

Updated D entry
(Frink)
(Updated D entry)
Line 376:
 
=={{header|D}}==
<lang d>import std.stdio, std.regexpregex;
 
void main() {
immutable string s = "I am a string";
 
// Test:
if (search!match(s, r"string$").empty)
writeflnwriteln("Ends with 'string'.");
 
// Test, storing the regular expression:
auto re1 = RegExp(r"string$");
if (re1.search(s).test)
writefln("Ends with 'string'");
 
// Substitute:
writefln(subreplace(s, regex(" a "), " another ").writeln();
 
// Substitute, storing the regular expression:
auto re2 = RegExp(" a ");
writefln(re2.replace(s, " another "));
}</lang>
{{out}}
 
writefln("<pre>Ends with 'string'");.
Note that in std.string there are string functions to perform those string operations in a faster way.
I am another string</pre>
Note that inIn std.string there are string functions to perform thosethe stringsame operations in a fastermore wayefficiently.
 
=={{header|Erlang}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.