digitalmars.D.learn - How to replace pairs tags with regexp
- Suliman (23/23) Jul 20 2017 I have got next code:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (30/50) Jul 20 2017 It's not guaranteed that the original document have space for the
- Suliman (1/2) Jul 20 2017 Thanks! I opened answer form before you answered me!
- Suliman (32/32) Jul 20 2017 Question above do not actual now. Now I have got next problem.
- Suliman (1/1) Jul 20 2017 There reason of issue above is spaces before "#".
- Suliman (8/9) Jul 21 2017 What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56
- Antonio Corbi (5/14) Jul 21 2017 Have you tried https://regex101.com/
- Suliman (3/22) Jul 21 2017 I tried. But I am getting different behavior in online editor and
- Antonio Corbi (3/28) Jul 21 2017 Could it be related to the regex's 'flavor' you are using?
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
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
AliThanks! I opened answer form before you answered me!
Jul 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 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
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
On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:Have you tried https://regex101.com/ It gives you results and explanations about your regex in realtime. A. CorbiWhat 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
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:I tried. But I am getting different behavior in online editor and in the code.On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:Have you tried https://regex101.com/ It gives you results and explanations about your regex in realtime. A. CorbiWhat 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
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:Could it be related to the regex's 'flavor' you are using? A. CorbiOn Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:I tried. But I am getting different behavior in online editor and in the code.On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:Have you tried https://regex101.com/ It gives you results and explanations about your regex in realtime. A. CorbiWhat 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