gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

I am reading in text from various files and need to pull out specific regex-s. I can easily perform the action to find the specific pattern I am looking for, but once I have it it is in a line of text delimited by varying characters and at a different offset from the beginning of the line each time.

Can someone tell me how I can get just this phrase from the line of text? What I need to do is find m/.*PNAME:.*/ in the string, once it is found I need to add just "PNAME:\w" to a hash. The part after the ":" will vary and I need to know how many occurances of each variation I have.

What I have so far is the right logic, but I end up with the whole line in $_ placed into the hash. I only want the PNAME:\w part.

I know I'm going to feel stupid when someone points this out, but HELP!.

TIA, Chad.

#!/usr/bin/perl -w use strict; my @filelist = < S2002* >; my %PNODES; for (@filelist) { open(FILE,$_); while (<FILE>) { if ($_ =~ m/.*PNODE:.*/) { $PNODES{$_} = 1; } } }

Replies are listed 'Best First'.
Re: Retrieving specific word from a line of text.
by impossiblerobot (Deacon) on Dec 11, 2002 at 16:48 UTC

    To answer your question, you need to use capturing parentheses. Additionally, the leading and trailing .*s are unnecessary.

    m/PNODE:(\w+)/

    The part of the regex matched within the parentheses will end up in $1. See perlre for more details.


    Updated: Added link to perldocs, and decided to assume that he wanted an entire word, rather than a single word character.


    Impossible Robot
Re: Retrieving specific word from a line of text.
by djantzen (Priest) on Dec 11, 2002 at 16:49 UTC

    You just need to capture that section and assign it to the hash. A capture is indicated by parentheses, and the results are stored in $1 - $9. So, try:

    if ($_ =~ m/.*(PNODE:\w*)/) # \w* assuming you want alphanumeric chars +. { $PNODES{$1} = 1; }

      As a matter of fact you are not limited to 9 capture variables $1 - $9.
      Perl will make as many $x-variables as needed.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law