CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
What kind of pattern match do I use ?

 



cosmokramer
Novice

Jun 12, 2012, 4:04 PM

Post #1 of 11 (2812 views)
What kind of pattern match do I use ? Can't Post

I would appreciate if someone can help me the pattern match format that would work best in this case.

I have a file that needs parsing which can have lines of this type:

lddfx__fishout_mnn2h_bit[5]

sliceA__fishtop_catch2h_bit[0]

mfff_slash_fishin_mnn2h_bit[15]

The word "fish" will definitely occur in the lines, but it could be fishin fishout, fishabove, etc.

I want to capture lines with the word "fish*_mnn2h_bit" ie; lines 1 & 3 above.


FishMonger
Veteran / Moderator

Jun 12, 2012, 4:18 PM

Post #2 of 11 (2810 views)
Re: [ckramer] What kind of pattern match do I use ? [In reply to] Can't Post

Untested:

Code
if ($line =~ /fish.+?_mnn2h_fit/) { 
print $line;
}



cosmokramer
Novice

Jun 12, 2012, 4:54 PM

Post #3 of 11 (2808 views)
Re: [FishMonger] What kind of pattern match do I use ? [In reply to] Can't Post

Here is the problem :

I will know what to look for only by parsing a 2nd file.

i.e - The 2nd file will tell me - look for lines with "fish*_mnn2h_bit"

with this how do I figure out that I need to print out lddfx__fishout_mnn2h_bit[5] & mfff_slash_fishin_mnn2h_bit[15] ?


cosmokramer
Novice

Jun 12, 2012, 5:26 PM

Post #4 of 11 (2806 views)
Re: [FishMonger] What kind of pattern match do I use ? [In reply to] Can't Post

btw this didn't work.

it couldn't find the pattern


(This post was edited by ckramer on Jun 12, 2012, 5:38 PM)


FishMonger
Veteran / Moderator

Jun 12, 2012, 7:40 PM

Post #5 of 11 (2800 views)
Re: [ckramer] What kind of pattern match do I use ? [In reply to] Can't Post


In Reply To
Here is the problem :

I will know what to look for only by parsing a 2nd file.

i.e - The 2nd file will tell me - look for lines with "fish*_mnn2h_bit"

with this how do I figure out that I need to print out lddfx__fishout_mnn2h_bit[5] & mfff_slash_fishin_mnn2h_bit[15] ?


Impossible to say without seeing how

Quote
The 2nd file will tell me - look for lines with "fish*_mnn2h_bit



rovf
Veteran

Jun 13, 2012, 1:40 AM

Post #6 of 11 (2781 views)
Re: [FishMonger] What kind of pattern match do I use ? [In reply to] Can't Post

Why a non-greedy match? Wouldn't just a .+ do as well here?


rovf
Veteran

Jun 13, 2012, 1:42 AM

Post #7 of 11 (2780 views)
Re: [ckramer] What kind of pattern match do I use ? [In reply to] Can't Post

How is the information stored in the 2nd file? I suggest you post a short (!) example of both files, and the code you already have.


FishMonger
Veteran / Moderator

Jun 13, 2012, 6:37 AM

Post #8 of 11 (2749 views)
Re: [rovf] What kind of pattern match do I use ? [In reply to] Can't Post


In Reply To
Why a non-greedy match? Wouldn't just a .+ do as well here?

Yes it will work, but not as efficiently. Using the greedy match will cause backtracking whereas the nongreedy match won't.


cosmokramer
Novice

Jun 13, 2012, 7:47 AM

Post #9 of 11 (2743 views)
Re: [FishMonger] What kind of pattern match do I use ? [In reply to] Can't Post

I think it is the special square braket(used for the bit [0] ) at the end that is causing the problem


Code
my $a = "slash_wideiscan"; my $b = "_mnn3h_reg[0]"; 
my @array1 = qw (slash_wideiscanps_mnn3h_reg[0]
slash_wideiscanptlr_mnn3h_reg[0]
slash_wideiscanps_mnn0h_reg);
for my $element (@array1) {
if ($element =~ /($a.+?$b)/) {
print "YES : $element\n";
} else {
print "NO : $element\n";
}
}



Answer that came back was

NO : slash_wideiscanps_mnn3h_reg[0]
NO : slash_wideiscanptlr_mnn3h_reg[0]
NO : slash_wideiscanps_mnn0h_reg

I actually want

YES : slash_wideiscanps_mnn3h_reg[0]
YES : slash_wideiscanptlr_mnn3h_reg[0]
NO : slash_wideiscanps_mnn0h_reg


FishMonger
Veteran / Moderator

Jun 13, 2012, 8:07 AM

Post #10 of 11 (2738 views)
Re: [ckramer] What kind of pattern match do I use ? [In reply to] Can't Post

$a and $b are special builtin global vars that are used in sort routines and it would be best to not use them outside of the sort routine.

Also, you should not have multiple statements on a single line.

You need to escape the [ ] brackets, which is done by using \Q


Code
my $str1 = "slash_wideiscan"; 
my $str2 = "_mnn3h_reg[0]";
my @array1 = qw (slash_wideiscanps_mnn3h_reg[0]
slash_wideiscanptlr_mnn3h_reg[0]
slash_wideiscanps_mnn0h_reg);

for my $element (@array1) {
if ($element =~ /$str1.+?\Q$str2\E/) {
print "YES : $element\n";
} else {
print "NO : $element\n";
}
}



(This post was edited by FishMonger on Jun 13, 2012, 8:08 AM)


cosmokramer
Novice

Jun 13, 2012, 9:00 AM

Post #11 of 11 (2734 views)
Re: [FishMonger] What kind of pattern match do I use ? [In reply to] Can't Post

Awesome. Thanks . I didn't know of this escape mechanism.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives