官术网_书友最值得收藏!

  • D Cookbook
  • Adam D. Ruppe
  • 479字
  • 2021-07-16 11:50:46

Searching with regular expressions

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.

How to do it…

Let's use the search operation with regular expressions by executing the following steps:

  1. Create a Regex object. If you are using string literals, D's r"" or `` syntax makes it much more readable.
  2. 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!");
}

How it works…

Regular expressions in D are a library type instead of a built-in type, but this doesn't affect their usability, their speed, or their simplicity. Declare a regular expression with the regex(pattern, flags) helper function. If you are using a string literal for the pattern, it will look nicer if you use one of D's raw string syntaxes, either r"pattern here" or `pattern here`. In those strings, the backslash doesn't have to be escaped, allowing the regular expression to look natural. The pattern syntax in D is almost similar to the syntax used in JavaScript.

After the regex is declared, you can use methods from std.regex to do all the basic tasks. The string.matchAll(pattern) function returns a list of matches in the string. You can check it in an if statement to see if it matches at all, or you can perform a foreach loop over it to get the details of the match.

The std.regex function also provides methods to replace instances, including a callback function, or to split a string. These functions work in the same way as replace and split from std.string.

There's more…

D's regular expressions are able to outperform most other regex engines by being processed at compile time. Instead of writing regex, write ctRegex! and make sure that the variable is static. The regular expression and the usage code do not have to be changed, as shown in the following code:

static ex = ctRegex!`[0-9]{3}-[0-9]{4}`;
foreach(match; "My phone number is 123-4567!".match(ex)) writeln(match);

The output is as follows:

["123-4567"]

With ctRegex, std.regex will then create D code to implement your specific regular expression and compile it, generating a fully optimized function to implement it. We'll learn how to create this kind of code later in the book.

See also

主站蜘蛛池模板: 喀喇沁旗| 仁化县| 克拉玛依市| 松原市| 德清县| 乳山市| 宁都县| 钟山县| 会泽县| 筠连县| 平江县| 永城市| 潜山县| 高密市| 平定县| 宜良县| 莒南县| 武宣县| 长白| 宝兴县| 呼伦贝尔市| 镇巴县| 平湖市| 丘北县| 无极县| 休宁县| 新泰市| 巫溪县| 天台县| 虹口区| 河曲县| 新干县| 贞丰县| 稻城县| 三江| 江口县| 广昌县| 林口县| 黎城县| 都江堰市| 新津县|