让我们看看Perl代码和结果: $s = "a\nb\nc\n";
$s =~ s/^b/X/;
print $s;
a
b
c
$s = "a\nb\nc\n";
$s =~ s/^b/X/m;
print $s;
a
X
c 我认为Perl是对的,只有在启用了multiline的情况下,^才会匹配中间新行之后的位置。 让我们看看Ruby: $s = "a\nb\nc\n"
print $s.sub(/^b/,'X')
a
X
c
$s = "a\nb\nc\n"
print $s.sub(/^b/m
File1.log Result for scripts as follows {"Script 1"=>{"expected"=>"Pass", "actual"=>"Pass", "result"=>true},
"Script 2"=>{"expected"=>"Fail", "actual"=>"Pass", "result"=>false}
-
我正在尝试读取一个文件并使用regex匹配多行,但遇到了一些问题。我尝试读取的文件如下所示:
I 09/07/20 05:55PM [Backup Set] Starting backup to CrashPlan Central: 122 files (93.30MB) to back up
I 09/07/20 06:00PM [Backup Set] Completed backup to CrashPlan Central in 0h:04m:39s: 147 files (197.90MB) backed up, 5.30MB encrypted and sent @ 323.5Kb
考虑以下多行字符串:
>> print s
shall i compare thee to a summer's day?
thou art more lovely and more temperate
rough winds do shake the darling buds of may,
and summer's lease hath all too short a date.
re.sub()将所有出现的and替换为AND
>>> print re.sub("and", "AND", s)
shall i c
我想提取与WhatsApp中的消息匹配的所有模式。消息的格式如下:
一行消息:
[19.09.17, 19:54:48] Marc: If the mean is not in the thousands, there's the problem
多行长消息:
[19.09.17, 19:54:59] Joe: > mean(aging$Population)
[1] 1593.577
Is what I get as solution
我能够将其拆分为日期、时间、发件人和消息,但只适用于一行代码,方法是先逐行读取文本文件,然后在不同的分隔符上拆分这些行。但是,这不适用于包含多行
。
我有一个类似于此的文本文件:
A
B
C
D
E
F
G
我需要:
在行E前移动行C;
将行C替换为X (X可以是多行);
将行G替换为Y (Y可以是多行);
删除D和F行。
这是我现在拥有的sed文件:
/C/ c\
X
/G/ c\
Y
/D/ d
/F/ d
哪一个做2,3,4,但不是1。我如何在C之前修复移动D24的脚本?
注意:我不能依赖行号或行的相邻性,这可能会有所不同;我只能依赖模式匹配。