On 07/20/2017 06:39 AM, Suliman wrote:
I have got next code:

import std.stdio;
import std.regex;
import std.file;

void main()
{
    auto text = readText("book.txt");

    auto inlineCodeBlock = regex("`([^`\n]+)`");
    auto bigCodeBlock = regex(r"`{3}[\s\S]*?`{3}");

    foreach(t; text.matchAll(bigCodeBlock))
    {
        string t1 = t.hit.replaceFirst(regex("`"),`<code>`);
        string t2 = t1.replaceFirst(regex("`"),`</code>`);
    }

}

Here I am replacing `foo` to <code>foo</code>. But got replaced data as
copy, not in original document. But I need to get replacing in original
document.

replaceAll is not suitable for it because it's not clear how to get open
and close tags (<code> and </code>).

It's not guaranteed that the original document have space for the modifications. I recommend outputting to a new place. This one build the output in memory, hoping that it will fit:

import std.stdio;
import std.regex;
import std.array;

void main()
{
    auto text = q"END
first line
```
void main() {
}
```
last line
END";

    auto bigCodeBlock = regex(r"`{3}([\s\S]*?)`{3}");

    auto result = appender!string;
    replaceAllInto(result, text, bigCodeBlock, "<code>$1</code>");
    writeln(result.data);
}

Outputs:

first line
<code>
void main() {
}
</code>
last line

Ali

Reply via email to