On 11/05/2011 16:38, jet speed wrote: > Hi All, > > I need help in matching the regular expression, the file is as below. > > I am trying to match number followed by Number ex 587, 128 in $1 and > 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11 in $2 > > the $1 match works find with regulare expression #if ($_=~ > /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line > > However i am not sure how to get both $1 and $2 togather. > > Ideally i would like to have an output printed > > print "$1 wwn is $2 \n"; > > Any help on this would be much appreciated. > > > FILE > ----------------------------------------------- > > > LOGICAL UNIT NUMBER 587 > UID: 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11 > > LOGICAL UNIT NUMBER 128 > UID: 60:06:01:60:50:40:21:00:D0:23:D5:C2:BA:D9:DE:11 > > LOGICAL UNIT NUMBER 763 > UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11 > > ----------------------------------------------------------- > > > #!/usr/bin/perl > open(FILE, "clrlist") || die "Can't open clrlist : $!\n"; > > while (<FILE>) { > #if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line > if ($_=~ > /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})\n[a-z0-9+]\s*(\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*)/i) > { > print "$1 "; > print "$2"; > } > } > > ---------------------
I think I understand what you hope to achieve, but your code will not compile as it stands. First of all, instead of /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i you should write simply /LOGICAL UNIT NUMBER (\d{1,4})/i Your second regex is similar, but anything like /\d{2}*/ gives the error 'Nested quantifiers in regex'. You can specify either {2} (exactly two occurrences) or * (zero or more occurrences) but not both! Also, you are trying to match a string of 16 hex bytes using /\d/. That matches only 0..9 in ASCII (and a lot more in Unicode, but that isn't a problem here). For hex, use either /[0-9A-X]/i or /[[:xdigit:]]/. But the main problem is that you are trying to match two lines simultaneously. Your regex contains /\n/, but you are reading from the file only one line at a time. Each line will contain *either* "LOGICAL UNIT NUMBER 587\n" *or* "UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11\n" so you need to code accordingly. On top of that, your second regex includes /[a-z0-9+]/i when I think you meant /[a-z0-9]+/i, and it doesn't allow for the colon after 'UID'. Once more, this should be /UID:\s*/ or something similar. Without seeing your data I cannot help further. For those who would help, I believe the program looks as below. I hope this helps, Rob use strict; use warnings; while (<DATA>) { #if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})\n[a-z0-9+]\s*(\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*)/i) { print "$1 "; print "$2"; } } __DATA__ LOGICAL UNIT NUMBER 587 UID: 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11 LOGICAL UNIT NUMBER 128 UID: 60:06:01:60:50:40:21:00:D0:23:D5:C2:BA:D9:DE:11 LOGICAL UNIT NUMBER 763 UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/