简体   繁体   English

合并多个代码块

[英]merging multiple code blocks together

I'm programming in JS. 我在用JS编程。 I want to but multiple same "code blocks" into one code block using regex. 我想使用正则表达式将多个相同的“代码块”放入一个代码块中。 Please note second block is another distinct block. 请注意, 第二个块是另一个不同的块。

Example Data 示例数据

BEFORE ENTRY
  Im on the top
  block
END

RANDOM NAME
  HAHA I MAKE A PROBLEM 
END

BEFORE ENTRY
  some randon
  strings with also 1 numbers 1
END

BEFORE ENTRY
  more strings
  very cool
END

BEFORE ENTRY
  stringi
  pizza
END

Desired Output 期望的输出

RANDOM NAME
  HAHA I MAKE A PROBLEM 
END

BEFORE ENTRY
  Im on the top
  block

  some randon
  strings with also 1 numbers 1

  more strings
  very cool

  stringi
  pizza
END

(i don't care whether RANDOM NAME is on top or on the bottom.) (我不在乎“随机名称”在顶部还是底部。)

My failed attempts 我的失败尝试

  1. /(?<=BEFORE ENTRY[^]*?)END[^]*?BEFORE ENTRY/g replace with "" (works if BEFORE ENTRY blocks are consequent) /(?<=BEFORE ENTRY[^]*?)END[^]*?BEFORE ENTRY/g替换为"" (如果因此进入入场前块,则可以使用)

  2. /(BEFORE ENTRY)([^]*?)(END)/g playing around with \\1 , \\2, \\3 . /(BEFORE ENTRY)([^]*?)(END)/g\\1 , \\2, \\3播放。 Would work if I could somehow merge the capturing groups. 如果我能以某种方式合并捕获组,则可以工作。

Edit regarding @Archer comment: Data-type is a multiline string inside output: . 编辑有关@Archer注释:数据类型是output:的多行字符串。

arr = [{id:"1", output:">>EXAMPLE DATA<<"},...,{id:"n", output:">>Some similar Data<<"}]

EDIT: sorry i didn't see the perl tag.. sould i remove the answer? 编辑:对不起,我没有看到perl标签。.我要删除答案吗? the logic is still re-usable.. 该逻辑仍然可以重用。

If you want to use regex, i would use the second one which is simpler, and capturing groups. 如果要使用正则表达式,我将使用第二个更简单的捕获组。 In addition, i made the solution generic with an array of keys. 此外,我通过一系列键使该解决方案通用。

Note that using a function for the replace , in addition to be able to control that the result is inserted only for the first match, also disables the special replacements such as $$ that could be in the result. 请注意,使用replace函数,除了能够控制仅在第一个匹配项中插入结果外,还禁用了可能出现在结果中的特殊替换,例如$$

You're not obliged to store results in an array, you could use a local var for result. 您不必将结果存储在数组中,可以将本地变量用于结果。

 var codeInput = `BEFORE ENTRY Im on the top block END RANDOM NAME HAHA I MAKE A PROBLEM END BEFORE ENTRY some randon strings with also 1 numbers 1 END BEFORE ENTRY more strings very cool END BEFORE ENTRY stringi pizza END`, ending = 'END', keys = ['BEFORE ENTRY', 'RANDOM NAME'], i, len = keys.length, reg, match, results = {}, inserted; for(i = 0; i < len; i++){ //getting all the contents reg = new RegExp(keys[i] + '([^]*?)' + ending, 'g'); while(match = reg.exec(codeInput)){ if(!results[keys[i]]){ results[keys[i]] = keys[i]; } results[keys[i]] += match[1]; } //inserting the result: replace the first one by result, remove others if(results[keys[i]]){ results[keys[i]] += ending + '\\n'; inserted = false; codeInput = codeInput.replace(reg , function(){ if(!inserted){ inserted = true; return results[keys[i]]; } return ''; }); } } document.getElementById('result').innerHTML = codeInput; 
 <pre id="result"><pre> 

Using perl 使用Perl

use warnings;
use strict;
my $f = 0;
my $before_entry;
while(<DATA>)
{
    $f = 1 ,  next if(/^BEFORE ENTRY/);
    print and  $f = 0 ,  if(/^END/ && $f == 2);
    $f = 0 if(/^END/);
    print and next if($f == 2);
    $f = 2  if(/^(?!BEFORE ENTRY|\s+)/ && $f != 1);
    $before_entry.=$_ if ($f == 1);
}

print "BEFORE ENTRY\n$before_entry\nEND\n"

__DATA__
BEFORE ENTRY
Im on the top
block
END

RANDOM NAME
HAHA I MAKE A PROBLEM 
END

BEFORE ENTRY
some randon
strings with also 1 numbers 1
END

BEFORE ENTRY
more strings
very cool
END

BEFORE ENTRY
stringi
pizza
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM