Home > Software design >  Need to grep a string from a line in a variable and store it in another variable [closed]
Need to grep a string from a line in a variable and store it in another variable [closed]

Time:09-22

I have a variable $abc that contains the line like below:

abc.txt ->  check a/b/c test

I need to get abc.txt in another variable say $bcd and a/b/c in the variable say $xyz. I have the regex to do so but I don't know how I can do it using perl as in my knowledge perl grep and perl map can be used on arrays only not variables.

CodePudding user response:

my ($bcd) = split(/\s*->\s*/, $abc, 2);
my $bcd = $abc =~ s/\s*->.*/sr;
my ($bcd) = $abc =~ /^(?:(?!\s*->).)*)/s;
my ($bcd) = $abc =~ /^(.*?)\s*->/s;

All but the last returns the entire input string if there's no ->.

  •  Tags:  
  • perl
  • Related