Ddoc $(D_S Regular Expressions, $(P Regular expressions are a powerful tool for pattern matching on strings of text. They are built in to the core of languages like Perl, Ruby, and Javascript. Perl and Ruby are particulary reknowned for adroitly handling regular expressions. So why aren't they part of the D core language? Read on and see how they're done in D compared with Ruby. ) $(P This article explains how to use regular expressions in D. It doesn't explain regular expressions themselves, after all, people have written entire books on that topic. D's specific implementation of regular expressions is entirely contained in the Phobos library module $(LINK2 phobos/std_regexp.html, std.regexp). For a more advanced treatment of using regular expressions in conjuction with template metaprogramming, see $(LINK2 templates-revisited.html, Templates Revisited). ) $(P In Ruby a regular expression can be created as a special literal: ) $(RUBY r = /pattern/ s = /p[1-5]\s*/ ) $(P D doesn't have special literals for them, but they can be created:) --- r = RegExp("pattern"); s = RegExp(r"p[1-5]\s*"); --- $(P If the $(I pattern) contains backslash characters \, wysiwyg string literals are used, which have the $(SINGLEQUOTE r) prefix to the string. $(I r) and $(I s) are of type $(B RegExp), but we can use type inference to declare and assign them automatically: ) --- auto r = RegExp("pattern"); auto s = RegExp(r"p[1-5]\s*"); --- $(P To check for a match of a string $(I s) with a regular expression in Ruby, use the =~ operator, which returns the index of the first match:) $(RUBY s = "abcabcabab" s =~ /b/ /* match, returns 1 */ s =~ /f/ /* no match, returns nil */ ) $(P In D this looks like: ) --- auto s = "abcabcabab"; std.regexp.find(s, "b"); /* match, returns 1 */ std.regexp.find(s, "f"); /* no match, returns -1 */ --- $(P Note the equivalence to std.string.find, which searches for substring matches rather than regular expression matches.) $(P The Ruby =~ operator sets some implicitly defined variables based on the result:) $(RUBY s = "abcdef" if s =~ /c/ "#{$`}[#{$&}]#{$'}" /* generates string ab[c]def ) $(P The function std.regexp.search() returns a RegExp object describing the match, which can be exploited: ) --- auto m = std.regexp.search("abcdef", "c"); if (m) writefln("%s[%s]%s", m.pre, m.match(0), m.post); --- $(P Or even more concisely as: ) --- if (auto m = std.regexp.search("abcdef", "c")) writefln("%s[%s]%s", m.pre, m.match(0), m.post); // writes ab[c]def ---