sedReplace

sed-replaces a line with a substitution string.

This clones the behaviour of the UNIX-like echo "foo" | sed 's/foo/bar/'.

version(WithSedReplacePlugin)
private
sedReplace
(
const string line
,
const string expr
,
const bool relaxSyntax
)

Parameters

line string

Line to apply the sed-replace pattern to.

expr string

Replacement pattern to apply.

relaxSyntax bool

Whether or not to require the expression to end with the delimiter.

Return Value

Type: auto

Original line with the changes the replace pattern incurred, or an empty string.init if nothing was changed.

Examples

string line = "This is a line";
string expression = "s/s/z/g";
assert(line.sedReplace(expression, relaxSyntax: false) == "Thiz iz a line");
1 {
2     enum before = "abc 123 def 456";
3     immutable after = before.sedReplace("s/123/789/", relaxSyntax: false);
4     assert((after == "abc 789 def 456"), after);
5 }
6 {
7     enum before = "I am a fish";
8     immutable after = before.sedReplace("s|a|e|g", relaxSyntax: false);
9     assert((after == "I em e fish"), after);
10 }
11 {
12     enum before = "Lorem ipsum dolor sit amet";
13     immutable after = before.sedReplace("s###g", relaxSyntax: false);
14     assert((after == "Lorem ipsum dolor sit amet"), after);
15 }
16 {
17     enum before = "高所恐怖症";
18     immutable after = before.sedReplace("s/高所/閉所/", relaxSyntax: false);
19     assert((after == "閉所恐怖症"), after);
20 }
21 {
22     enum before = "asdf/fdsa";
23     immutable after = before.sedReplace("s/\\//-/", relaxSyntax : false);
24     assert((after == "asdf-fdsa"), after);
25 }
26 {
27     enum before = "HARBL";
28     immutable after = before.sedReplace("s/A/_/", relaxSyntax : false);
29     assert((after == "H_RBL"), after);
30 }
31 {
32     enum before = "there are four lights";
33     immutable after = before.sedReplace("s@ @_@g", relaxSyntax : false);
34     assert((after == "there_are_four_lights"), after);
35 }
36 {
37     enum before = "kameloso";
38     immutable after = before.sedReplace("s los bot ", relaxSyntax : false);
39     assert((after == "kameboto"), after);
40 }
41 {
42     enum before = "abc 123 def 456";
43     immutable after = before.sedReplace("s/123/789", relaxSyntax : true);
44     assert((after == "abc 789 def 456"), after);
45 }
46 {
47     enum before = "高所恐怖症";
48     immutable after = before.sedReplace("s/高所/閉所", relaxSyntax : true);
49     assert((after == "閉所恐怖症"), after);
50 }
51 {
52     enum before = "asdf/fdsa";
53     immutable after = before.sedReplace("s/\\//-", relaxSyntax : true);
54     assert((after == "asdf-fdsa"), after);
55 }
56 {
57     enum before = "HARBL";
58     immutable after = before.sedReplace("s/A/_/", relaxSyntax : true);
59     assert((after == "H_RBL"), after);
60 }
61 {
62     enum before = "kameloso";
63     immutable after = before.sedReplace("s los bot", relaxSyntax : true);
64     assert((after == "kameboto"), after);
65 }