Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用perl将文本文件分成几个块_Regex_Perl - Fatal编程技术网

Regex 用perl将文本文件分成几个块

Regex 用perl将文本文件分成几个块,regex,perl,Regex,Perl,我有一个包含diff输出的文件,如下所示: 26c26 < Or in the bright light of morning --- > Or in the bright light of evning 34c34,36 < clouds will roll back clear of the summits. --- > hello world > I came here to ask > some questions 43,44c45 <

我有一个包含diff输出的文件,如下所示:

26c26
< Or in the bright light of morning 
---
> Or in the bright light of evning 
34c34,36
< clouds will roll back clear of the summits. 
---
> hello world
> I came here to ask
> some questions 
43,44c45
< How to imagine an orchid boat? 
< It gets harder. But days come and go, 
---
> don't you cry tonigh, 
49,50c50,52
< though only caught that imprisoning moment. 
< A golden leaf in autumn spins into a dark river 
---
> che che che che changes. 
> la la la la la 
> good song
26c26
<或在清晨明亮的光线下
---
>或者在埃夫宁的亮光下
34c34,36
云会从山顶上滚回来。
---
>你好,世界
>我是来问你的
>一些问题
43,44c45
<如何想象一艘兰花船?
<越来越难了。但是日子一天天过去,
---
>今晚不要哭,
49,50c50,52
<虽然只抓住了那一关。
<秋天的一片金叶旋转成一条黑暗的河流
---
>车在变。
>啦啦啦啦
>好歌
i wnat以逐块处理此文件,其中twho块由以下格式的delimeter重新处理:

{number}c{number}例如48c70

{number},{number}c{number}例如90120c70

{number}、{number}c{number}、{number}例如36,40c881020

{number}c{number},{number}例如99100C70

例如,上面的文件包含4个块:

第1区:

26c26
< Or in the bright light of morning 
---
> Or in the bright light of evning 
26c26
<或在清晨明亮的光线下
---
>或者在埃夫宁的亮光下
第2区:

34c34,36
< clouds will roll back clear of the summits. 
---
> hello world
> I came here to ask
> some questions 
34c34,36
云会从山顶上滚回来。
---
>你好,世界
>我是来问你的
>一些问题
第3区:

43,44c45
< How to imagine an orchid boat? 
< It gets harder. But days come and go, 
---
> don't you cry tonigh, 
43,44c45
<如何想象一艘兰花船?
<越来越难了。但是日子一天天过去,
---
>今晚不要哭,
第4区:

49,50c50,52
< though only caught that imprisoning moment. 
< A golden leaf in autumn spins into a dark river 
---
> che che che che changes. 
> la la la la la 
> good song
49,50c50,52
<虽然只抓住了那一关。
<秋天的一片金叶旋转成一条黑暗的河流
---
>车在变。
>啦啦啦啦
>好歌
如何使用Perl实现这一点


谢谢

基于

(?=^(\d+,)?\d+c\d+(,\d+)?$)

这里是

因为您只是处理来自的输出,所以只需查找以数字开头的行:

my @blocks = split /^(?=\d)/m, do {local $/; <DATA>};

use Data::Dump;
dd @blocks;

__DATA__
26c26
< Or in the bright light of morning 
---
> Or in the bright light of evning 
34c34,36
< clouds will roll back clear of the summits. 
---
> hello world
> I came here to ask
> some questions 
43,44c45
< How to imagine an orchid boat? 
< It gets harder. But days come and go, 
---
> don't you cry tonigh, 
49,50c50,52
< though only caught that imprisoning moment. 
< A golden leaf in autumn spins into a dark river 
---
> che che che che changes. 
> la la la la la 
> good song
my@blocks=split/^(?=\d)/m,do{local$/;};
使用数据::转储;
dd@块;
__资料__
26c26
<或在清晨明亮的光线下
---
>或者在埃夫宁的亮光下
34c34,36
云会从山顶上滚回来。
---
>你好,世界
>我是来问你的
>一些问题
43,44c45
<如何想象一艘兰花船?
<越来越难了。但是日子一天天过去,
---
>今晚不要哭,
49,50c50,52
<虽然只抓住了那一关。
<秋天的一片金叶旋转成一条黑暗的河流
---
>车在变。
>啦啦啦啦
>好歌
产出:

(
  "26c26\n< Or in the bright light of morning \n---\n> Or in the bright light of evning \n",
  "34c34,36\n< clouds will roll back clear of the summits. \n---\n> hello world\n> I came here to ask\n> some questions \n",
  "43,44c45\n< How to imagine an orchid boat? \n< It gets harder. But days come and go, \n---\n> don't you cry tonigh, \n",
  "49,50c50,52\n< though only caught that imprisoning moment. \n< A golden leaf in autumn spins into a dark river \n---\n> che che che che changes. \n> la la la la la \n> good song",
)
(
“26c26\n<或在早晨的明亮光线下\n---\n>或在埃夫宁的明亮光线下\n”,
“34c34,36\n<云层将从峰顶上滚落。\n---\n>你好,世界\n>我来这里是想问一些问题\n>”,
“43,44c45\n<如何想象一艘兰花船?\n<越来越难了。但是日子来了又走,\n---\n>你今天不要哭了,\n”,
“49,50c50,52\n<虽然只抓住了那个囚禁的时刻。\n<秋天的一片金叶旋转成一条黑暗的河流\n---\n>车辙的变化。\n>拉拉拉拉\n>好歌”,
)

也许这对您有所帮助:?更简单、更高效:
split/^(?=[^-])/m
根据分隔符的解释,我更喜欢使用它,因为我没有关于其他行的上下文。它可以是任何只有OP知道的东西。根据提供的信息,他说这是
diff
的输出(如果你曾经使用过这个工具,这也是显而易见的)。