Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I like to do a pattern match and get the ouptut as FILE_INPUT_wrt004.
I have tried this following, But it did not help,
my $inc = "13 drwxr-xr-x 4 root root 34816 Mar 18 08:17 FI +LE_INPUT_Wrt004)"; if ($inc =~/FILE_INPUT/){ print"Inc is $inc"; } Output I got: Inc is 13 drwxr-xr-x 4 root root 34816 Mar 18 08:17 FILE_I +NPUT_Wat004 Expected output: FILE_INPUT_Wrt004
Can you help me on this please?
Thank you.

Replies are listed 'Best First'.
Re: Help on pattern match
by ikegami (Patriarch) on Mar 18, 2009 at 13:05 UTC

    You don't change $inc, so no surprise it contains the whole line.

    if (my ($file) = $inc =~ /(FILE_INPUT_\w+)/) { print("$file\n"); }

    Update: Replaced print("$inc\n"); as per reply.

      Don't you mean:
      if (my ($file) = $inc =~ /(FILE_INPUT_\w+)/) { print("$file\n"); }

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks ikegami. It solved my problem.
Re: Help on pattern match
by Corion (Patriarch) on Mar 18, 2009 at 13:09 UTC

    You're close. You just need to tell Perl what parts of the line you want to capture - how else would Perl know that you're not just interested in FILE_INPUT but also stuff following it? See perlre on how to capture things into $1. Basically the following RE should give you a start:

    /(FILE_INPUT\w+)/

    If the match is successful (and only then), the captured value will be stored in $1.