www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to replace pairs tags with regexp

reply Suliman <evermind live.ru> writes:
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>).
Jul 20 2017
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jul 20 2017
parent Suliman <evermind live.ru> writes:
 Ali
Thanks! I opened answer form before you answered me!
Jul 20 2017
prev sibling parent reply Suliman <evermind live.ru> writes:
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


     my sub header text


     my sub 3 text

     #Header2
     my header2 text";


'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


my sub header text

?

I tried different combination, some of them are working in online 
regexp editors, but do not working in D.
Jul 20 2017
parent reply Suliman <evermind live.ru> writes:

Jul 20 2017
parent reply Suliman <evermind live.ru> writes:
On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:

What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56 I expect that it will select: #Header my header text my sub header text
Jul 21 2017
parent reply Antonio Corbi <aaacorbi mail.com> writes:
On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:
 On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:

What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56 I expect that it will select: #Header my header text my sub header text
Have you tried https://regex101.com/ It gives you results and explanations about your regex in realtime. A. Corbi
Jul 21 2017
parent reply Suliman <evermind live.ru> writes:
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:

What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56 I expect that it will select: #Header my header text my sub header text
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.
Jul 21 2017
parent Antonio Corbi <amcb ggmail.com> writes:
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:

What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56 I expect that it will select: #Header my header text my sub header text
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
Jul 21 2017