Perl 我需要替换主题,例如:-主题:****但它正在输出:-主题:****计算机

Perl 我需要替换主题,例如:-主题:****但它正在输出:-主题:****计算机,perl,Perl,在这里,您可以看到我的代码正在用****** 但现在的情况是,它并没有取代第二条线路。 我一直在尝试这样做,但每次都失败了。如果有人能帮我,请查收 我想要的输出是: Name : Piyush Prasad Class : 12th Stream : Science Subjects :***** Text : what ever happens to this code does not even matter to me Time :***** 我得到的结果是: Name : Piyus

在这里,您可以看到我的代码正在用
******
但现在的情况是,它并没有取代第二条线路。 我一直在尝试这样做,但每次都失败了。如果有人能帮我,请查收

我想要的输出是:

Name : Piyush Prasad
Class : 12th
Stream : Science
Subjects :*****
Text : what ever happens to this code does not even matter to me 
Time :*****
我得到的结果是:

Name : Piyush Prasad
Class : 12th
Stream : Science
Subjects :*****Computer
Text : what ever happens to this code does not even matter to me 
Time :***** 
代码如下:

 my $fm_log= "
 -------------------------------------------------------------------------------
 Name : Piyush Prasad
 Class : 12th
 Stream : Science
 Subjects : Physics Chemistry Maths
            Computer
 Text : what ever happens to this code does not 
        even matter to me 
 Time : 10.304534
 ";
 my %fm_info;
 $fm_log =~ s!.*?(---------------)!$1!s;
 $fm_log =~ s!vwmg.*!!sg;
 my @fm_log = split(/-------+/, $fm_log);
 @fm_log = grep {! m!^\s*$!s} @fm_log;
 for (my $i = 0; $i < @fm_log;$i++){
     my $type = undef;
     my @tmp1 = split(/\R+/,$fm_log[$i]);
     foreach (@tmp1)
       {
         $_ =~ s!^\s+!xxxx! if $_ !~ m!:!;
         $_ =~ s!(Time\s+:)\s+.*!${1}xxx!;
         $_ =~ s!(Subjects\s+:)\s+.*!${1}xxx!;
       }
      $fm_log[$i] = join("\n",@tmp1);
      $fm_log[$i] =~ s!\R+xxxx!!mg;
       }
      print @fm_log;
my$fm_log=”
-------------------------------------------------------------------------------
姓名:皮尤斯·普拉萨德
班级:12
流:科学
科目:物理化学数学
电脑类
文本:此代码发生的任何事情都不会
甚至对我来说都很重要
时间:10:304534
";
我的%fm_信息;
$fm_log=~s!*?(---------------)!$1.s
$fm_log=~s!vwmg。*!!sg;
my@fm_log=split(/----+/,$fm_log);
@fm_log=grep{!m!^\s*$!s}@fm_log;
对于(我的$i=0;$i<@fm_log;$i++){
my$type=undf;
my@tmp1=split(/\R+/,$fm_log[$i]);
foreach(@tmp1)
{
$\uM=~s!^\s+!xxxx!如果$\uM!:!;
$\=~s!(时间\s+:)\s+.*!${1}xxx!;
$\=~s!(主题\s+:)\s+.*!${1}xxx!;
}
$fm_log[$i]=加入(“\n”,@tmp1);
$fm_log[$i]=~s!\R+xxxx!!mg;
}
打印@fm_日志;

您得到的原因

Subjects :*****Computer
而不是

Subjects :*****
就是在换行符
\R+
上拆分原始字符串,然后编辑这些行,然后将字符串重新连接在一起。由于
计算机
位于新行,因此不会对其进行编辑

 my @tmp1 = split(/\R+/,$fm_log[$i]);         # <---- splitting the line
 foreach (@tmp1) {                            # editing the pieces
     $_ =~ s!^\s+!xxxx! if $_ !~ m!:!;
     $_ =~ s!(Time\s+:)\s+.*!${1}xxx!;
     $_ =~ s!(Subjects\s+:)\s+.*!${1}xxx!;
 }
 $fm_log[$i] = join("\n",@tmp1);              # joining the pieces
然后在换行符上拆分字符串,并尝试删除
foo:

my @pieces = split /\R+/, $str;
现在
@pieces
将包含以下内容:

$VAR1 = [
      'foo: bar',
      'baz'
    ];
如果我们试图用
s/foo:.*/
编辑它们,我们将永远不会影响原始字符串的
baz
部分

由于您有一个文件,其中包含的字段由以单词开头的行分隔,后跟冒号,后跟可能包含换行符的内容,因此您需要首先将字段彼此分隔开

你的领域看起来像

TITLE : CONTENT
        CONTENT ...
TITLE : CONTENT ...
可能有更适合读取此格式的模块。我编造了这个快速破解,将数据强制转换成一个散列,您可以通过简单地为字段分配新值来编辑字段

use strict;
use warnings;
use Data::Dumper;

my $fm_log= " Name : Piyush Prasad
 Class : 12th
 Stream : Science
 Subjects : Physics Chemistry Maths
            Computer
 Text : what ever happens to this code does not 
        even matter to me 
 Time : 10.304534
 ";

my @info = split /^ *([\w ]+) : /m, $fm_log;   # split into fields
s/\s+/ /g for @info;                           # cleanup start
s/^\s+|\s+$// for @info;
shift @info;                                   # cleanup complete
my %info = @info;
print Dumper \%info;                 # show the data structure we made

$info{Subjects} = "****";
$info{Time} = "****";

printf("%s : %s\n", $_, $info{$_}) for qw(Name Class Stream Subjects Text Time);
这将输出

$VAR1 = {
          'Class' => '12th',
          'Stream' => 'Science',
          'Time' => '10.304534',
          'Subjects' => 'Physics Chemistry Maths Computer',
          'Text' => 'what ever happens to this code does not even matter to me',
          'Name' => 'Piyush Prasad'
        };
Name : Piyush Prasad
Class : 12th
Stream : Science
Subjects : ****
Text : what ever happens to this code does not even matter to me
Time : ****

实际上,代码中没有文字星号
**
。请显示您的真实代码。您有包含换行符的字段,“字段分隔符”是指一个新字段以一个单词开头,后跟一个冒号。因此,除非先分离字段,否则不能使用
*
之类的东西来删除内容。我假设您不是试图通过先键入字符串然后再更改字符串的方式来编辑字符串,而是从文件中读取字符串。我建议详细说明这一点,以获得一个符合您需求的完整解决方案。
$VAR1 = {
          'Class' => '12th',
          'Stream' => 'Science',
          'Time' => '10.304534',
          'Subjects' => 'Physics Chemistry Maths Computer',
          'Text' => 'what ever happens to this code does not even matter to me',
          'Name' => 'Piyush Prasad'
        };
Name : Piyush Prasad
Class : 12th
Stream : Science
Subjects : ****
Text : what ever happens to this code does not even matter to me
Time : ****