-
Book Overview & Buying
-
Table Of Contents
D Cookbook
By :
Regular expressions are a common tool to perform advanced operations on text, including complex searches, replacements, splitting, and more. Unlike JavaScript or Perl, D does not have regular expressions built into the language, but it has all the same power—and more—as those languages through the std.regex Phobos module. To explore regular expressions in D, we'll write a small program that searches stdin for a particular regex that is given on the command line and prints out the matching lines.
Let's use the search operation with regular expressions by executing the following steps:
Create a Regex object. If you are using string literals, D's r"" or `` syntax makes it much more readable.
Loop over the matches and print them out.
Pretty simple! The code is as follows:
void main(string[] args) {
import std.regex, std.stdio;
auto re = regex(args[1], "g");
foreach(line; stdin.byLine)
if(line.match(re)) writeln(line, " was a match!"...
Change the font size
Change margin width
Change background colour