Thread overview
How to replace pairs tags with regexp
Jul 20, 2017
Suliman
Jul 20, 2017
Ali Çehreli
Jul 20, 2017
Suliman
Jul 20, 2017
Suliman
Jul 21, 2017
Suliman
Jul 21, 2017
Suliman
Jul 21, 2017
Antonio Corbi
Jul 21, 2017
Suliman
Jul 21, 2017
Antonio Corbi
July 20, 2017
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>).
July 20, 2017
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

July 20, 2017
Question above do not actual now. Now I have got next problem.

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

void main()
{
    auto text = "#Header
    my header text

    ##SubHeader
    my sub header text

    ###Sub3Header
    my sub 3 text

    #Header2
    my header2 text";

    auto l1 = regex(`(^|\n)#([^#]*)\n([\^]*)(?=\n#[^#]|$)`, ['g', 'm']);

    foreach(t; text.matchAll(l1))
    {
        writeln(t.hit);
    }
}


This code is print to console:

#Header
my header text

How can I modify regex to get it print:

#Header
my header text

##SubHeader
my sub header text

?

I tried different combination, some of them are working in online regexp editors, but do not working in D.
July 20, 2017
> Ali
Thanks! I opened answer form before you answered me!


July 21, 2017
There reason of issue above is spaces before "#".
July 21, 2017
On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:
> There reason of issue above is spaces before "#".

What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56

I expect that it will select:

#Header
my header text

##SubHeader
my sub header text

Because: ^#{3}
July 21, 2017
On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:
> On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:
>> There reason of issue above is spaces before "#".
>
> What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56
>
> I expect that it will select:
>
> #Header
> my header text
>
> ##SubHeader
> my sub header text
>
> Because: ^#{3}

Have you tried https://regex101.com/

It gives you results and explanations about your regex in realtime.

A. Corbi
July 21, 2017
On Friday, 21 July 2017 at 07:30:07 UTC, Antonio Corbi wrote:
> On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:
>> On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:
>>> There reason of issue above is spaces before "#".
>>
>> What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56
>>
>> I expect that it will select:
>>
>> #Header
>> my header text
>>
>> ##SubHeader
>> my sub header text
>>
>> Because: ^#{3}
>
> Have you tried https://regex101.com/
>
> It gives you results and explanations about your regex in realtime.
>
> A. Corbi

I tried. But I am getting different behavior in online editor and in the code.
July 21, 2017
On Friday, 21 July 2017 at 07:42:28 UTC, Suliman wrote:
> On Friday, 21 July 2017 at 07:30:07 UTC, Antonio Corbi wrote:
>> On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:
>>> On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:
>>>> There reason of issue above is spaces before "#".
>>>
>>> What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56
>>>
>>> I expect that it will select:
>>>
>>> #Header
>>> my header text
>>>
>>> ##SubHeader
>>> my sub header text
>>>
>>> Because: ^#{3}
>>
>> Have you tried https://regex101.com/
>>
>> It gives you results and explanations about your regex in realtime.
>>
>> A. Corbi
>
> I tried. But I am getting different behavior in online editor and in the code.

Could it be related to the regex's 'flavor' you are using?

A. Corbi