Perl: Find a line in a txt by combining typed words?

5

Good afternoon,

I have a notepad with several lines typed as for example:

★ Bayonet
★ Bayonet | Autotronic (Battle-Scarred)
★ Bayonet | Blue Steel (Minimal Wear)
★ Bayonet | Case Hardened (Battle-Scarred)
★ Bayonet | Case Hardened (Factory New)
★ Bayonet | Case Hardened (Field-Tested)

In my code one of the lines will wait for input on the keyboard asking what name you want to find

use strict;
open(FILEHANDLE,'<','filtradas.txt') or die "Arquivo não pode ser aberto\n";
my @linhas = <FILEHANDLE>;
close(FILEHANDLE);
print "Informe o nome do item que deseja encontrar: ";
my $nome = <STDIN>; chomp($nome);
foreach my $linhas(@linhas) {
chomp($linhas);
if($linhas =~ m/$nome/gi) {
    print $linhas."\n";
}
}

The question is: Is there any way to create a regex that if for example I type in the keyboard input "Bayonet Case" it will print me all the results of the notepad that contains Bayonet + Case on the same line? In case:

★ Bayonet | Case Hardened (Battle-Scarred)
★ Bayonet | Case Hardened (Factory New)
★ Bayonet | Case Hardened (Field-Tested)

I'm sorry if I could not be clear, if you did not understand I'd try to explain it some other way.

Thank you in advance.

    
asked by anonymous 22.05.2017 / 21:38

1 answer

1

Problem solved with the following function:

$nome =~ s/\s+/.*/g;

What turns "bayonet case" into ("bayonet. * case".).

Thanks Hernan.

    
24.05.2017 / 01:11