How do you map regex string replacement values ($1,$2 etc) to a hash?

my (@keys,@values) = ($text =~ /\{IS\:([a-zA-Z0-9_-]+)\}(.*)\{\\IS\:([a-zA-Z0-9_-]+)\}/g);

is supposed to match strings like this

{IS:dog}meow{\IS:dog} #probably not a dog

which works fine, except that all the $1,$2, and $3 value get dumped into @keys .. so I'm trying to figure out how to get these guys into a nice hash of $1 => $2 pairs...

For full context what I'd really like to do however is have the regex expression return a data structure that looks like (and append a count for the number of times the key was found)

  cow_1 => moo,
  cow_2 => moo,
  dog_1 => bark,
  dog_2 => meow,

Is there a way to use map{ } function to accomplish this with Regex? Something like this maybe?

my %datahash = map { ( $1 eq $3 ) ? { $1 => $2 } : undef } @{ regex...};

$1 equals $3 to make sure its a matching tag (no need for recursive checking these tags aren't nested), if so use $1 as the key and $2 as the value;

Then for each of these key => value pairs, i want to replace




then if $cachedData{cow} is true all cow_* will be replaced with their key in %datahash...


I removed useless backslashes and parens from the regex and used shortcuts in the char class:

use warnings;
use strict;

my $text = '{IS:cow}moo{\IS:cow}

my %cnt;
my %animals;
while ( $text =~ /\{IS:([\w-]+)}(.*)\{\\IS:[\w-]+}/g ){
    $animals{$1 . '_' . ++$cnt{$1}} = $2;

print "$_ => $animals{$_}\n" for sort keys %animals;

$hash{$1} = $2 while 
        $text =~ /\{IS\:([a-zA-Z0-9_-]+)\}

(/x modifier added for readability)

Need Your Help

Saving each WAV channel as a mono-channel WAV file using Naudio

c# audio naudio waveform wave

I'm trying to convert a WAV file(PCM,48kHz, 4-Channel, 16 bit) into mono-channel WAV files.

Stop SQL Server Management Studio from adding comment to stored procedures

sql-server sql-server-2008 ssms

How do I prevent SQL Management Studio (10.50.2500.0) from adding this to the beginning of every stored procedure when I right-click/Modify them?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.