in reply to Regular Expression - print out certain sections of line based on the content

It would be far more use to you (and indeed, us) if you were to provide ...

Having said that, this may do something like what you're after (untested code alert)..

my @strings = ( "ENV(ABCD1234) STATUS(Running)", "ENV(IJK1234) STATUS(Not Running)", "PRINT NAME FIRST(ABCD) SECOND(EFGH) ADDRESS('') PHONE(12345678)", "PRINT NAME FIRST() SECOND(WXYD) ADDRESS('') PHONE(87654321)" ); foreach (@strings) { print $1 if /ENV\(([^)]*)\) STATUS\(Not Running\)/; print $1,$2 if /^PRINT NAME FIRST\(([^)]*)\).* PHONE\([^)]*\)/; }

Update:

Modded RE (to remove space after 'FIRST' - thanx to ww.

A user level that continues to overstate my experience :-))
  • Comment on Re: Regular Expression - print out certain sections of line based on the content
  • Download Code

Replies are listed 'Best First'.
Re^2: Regular Expression - print out certain sections of line based on the content
by ww (Archbishop) on Jan 02, 2009 at 17:23 UTC

    "remove space after "FIRST'" ++

    But the output from your code above is:

    IJK1234 Use of uninitialized value in print at F:\_wo\pl_test\733761b2.pl line + 16. ABCD Use of uninitialized value in print at F:\_wo\pl_test\733761b2.pl line + 16.

    ...which doesn't capture the phone number.

    So, perhaps one might wish to capture the phone in the manner below and avoid the uninitialized warnings re the case where "FIRST" is empty:

    foreach my $item(@strings) { print "in ENV Not Running: " . $1 if ($item =~ /ENV\(([^)]*)\) STATU +S\(Not Running\)/); $item =~ /^PRINT NAME FIRST\(([^)]*)\).* PHONE\((.+)\)/; if ($1 && $2) { print "in PRINT NAME FIRST: $1, " . $2; } }

    OUTPUT:

    in ENV Not Running: IJK1234 in PRINT NAME FIRST: ABCD, 12345678

    Note that adding the labels to the output may bollix further processing, depending on where OP is going with this.

Re^2: Regular Expression - print out certain sections of line based on the content
by jas999 (Initiate) on Jan 02, 2009 at 16:21 UTC
    Thank you for this. My reg expression was incorrect