Perl script to search and extract using wildcards.

Good evening All,

I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. This script needs to search for more than one occurrence per line regardless of the position or surrounding text.

Desired output:

xx1.p
xx3.p
xx4.p
asdfxx.p
#!/usr/bin/perl -w
$test = "xx1.p  xx2.r \n (xx3.p) ( xx4.p ) \n xx5.r xx.p.pxx \n asdfxx.p \n xxixiadsf.xx.p  \n xx4.p {xx5.p}   (  xx6.p)";
while ($test =~ /(xx.*\.p)/g) {
       print "$1\n";
}

Output

xx1.p
xx3.p) ( xx4.p
xx5.r xx.p.p
xx.p
xxixiadsf.xx.p
xx4.p {xx5.p}   (  xx6.p

Any help would be appreciated.
Thank you,
Cammy

The biggest problem with your regexp is the greedy match .* but your search criteria is too vague to want to try and post any code. For example, why does asdfxx.p but not xx5.p? Of course your regexp will not match asdfxx.p anyway. Please post some real sample data and explain the search criteria in more detail.

Drop boundaries on the regex and replace the .* with a less greedy \S*:

#!/usr/bin/perl -w

$test = "xx1.p  xx2.r \n (xx3.p) ( xx4.p ) \n xx5.r xx.p.pxx \n asdfxx.p \n xxix
iadsf.xx.p  \n xx4.p {xx5.p}   (  xx6.p)";

while ($test =~ /\b(xx\S*\.p)\b/g) {
       print "$1\n";
}

Output:
xx1.p
xx3.p
xx4.p
xxixiadsf.xx.p
xx4.p
xx5.p
xx6.p

I'm with KevinADC... I'm a bit confused how asdfxx.p is expected to be part of the output when you say "of a files beginning with xx".

Both of you were correct. I inadvertantly left the asdfxx.p file in the "desired output". I indeed, did not want it to find that match.
I made the changes, as Mumford suggested, and it worked exactly as I wanted it to originally. Thank you both for your expertise!:D:D:D