Regular expressions: Difference between revisions

Content added Content deleted
(Frink)
(Updated D entry)
Line 376: Line 376:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.regexp;
<lang d>import std.stdio, std.regex;


void main() {
void main() {
string s = "I am a string";
immutable string s = "I am a string";


// Test:
// Test:
if (search(s, r"string$"))
if (!match(s, r"string$").empty)
writefln("Ends with 'string'");
writeln("Ends with 'string'.");

// Test, storing the regular expression:
auto re1 = RegExp(r"string$");
if (re1.search(s).test)
writefln("Ends with 'string'");


// Substitute:
// Substitute:
writefln(sub(s, " a ", " another "));
replace(s, regex(" a "), " another ").writeln();

// Substitute, storing the regular expression:
auto re2 = RegExp(" a ");
writefln(re2.replace(s, " another "));
}</lang>
}</lang>
{{out}}

<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>
In std.string there are string functions to perform the same operations more efficiently.


=={{header|Erlang}}==
=={{header|Erlang}}==