
Kenosis
User
Mar 22, 2013, 6:41 AM
Post #9 of 15
(4072 views)
|
I think rovf is right on target. Also don't use $& as it has a performance cost within Perl. Use the results of your capture that's contained in $1. Consider the following:
use warnings; use strict; use Data::Dumper; my @matches; while ( my $line = <DATA> ) { while ( $line =~ /\{(.*?)\}/g ) { push @matches, $1 if $1 and $1 !~ /\*/; } } print Dumper \@matches; __DATA__ some useless text {"to_id": 0, "message": "This is a sample", "message_id": 1000, "from_id": 999} more useless text {"to_id": 1, "message": "This is a sample", "message_id": 1000, "from_id": 999} and more some useless text {"to_id": 3, "message": "*This is a sample", "message_id": 1000, "from_id": 999} more useless text {"to_id": 4, "message": "This is a sample", "message_id": 1000, "from_id": 999} and more Output:
$VAR1 = [ '"to_id": 0, "message": "This is a sample", "message_id": 1000, "from_id": 999', '"to_id": 1, "message": "This is a sample", "message_id": 1000, "from_id": 999', '"to_id": 4, "message": "This is a sample", "message_id": 1000, "from_id": 999' ]; The second regex just checks whether "*" is contained in your capture. The $1 and $1 !~ /\*/ notation just makes sure that you did, in fact, capture something, since you could 'capture' an empty string between "{}" and it would be true that "*" isn't within "". Hope this helps!
|