in reply to Regex random values pattern match and code efficiency

Trying to understand your question: In the first post you were told how to find the right hand side of DEPLOY_PARAMS=$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG} and now you want to extract what's within the braces? Like this:

use strict; use warnings; my $str='$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG}'; my @result = $str =~ /{(.+?)}/g; print "@result\n";

Update: Putting it all together:

use strict; use warnings; use Data::Dumper; my %h = map {my($k, $v)=/^(.*?)=(.*)/; $v=[ $v=~/{(.+?)}/g ] if $v=~/} +/; $k=>$v } <DATA>; print Dumper \%h; __DATA__ DEPLOY_TIME=300 DEPLOY_TYPE=OS DEPLOY_PARAMS=$DOMAIN{HOST},$DOMAIN{IP},$DOMAIN{CONFIG} DEPLOY_DEFAULT_STARTUP=$DOMAIN{STARTUP.INI} DEPLOY_DEBUG=Y DEFAULT_EXPIRY=900

Replies are listed 'Best First'.
Re^2: Regex random values pattern match and code efficiency
by Mark.Allan (Sexton) on Sep 12, 2013 at 18:04 UTC

    Yes, extract values which are in braces only BUT the same values will not be there every time, for example, another file will only contain the following

    DEPLOY_PARAMS=$DOMAIN{HOST},$DOMAIN{IP}

    Or

    DEPLOY_PARAMS=$DOMAIN{HOST}

    For example, the values could change in every file so its not static mapping

      Well, I encourage you to run my proposed code on all kinds of test data to see how it fares. You might also try to understand how it works to see that it does not care what exactly there is. As you will see there is no sign of a static mapping.

      One thing I would propose is to add a filter for lines without equal sign to make it more robust:

      my %h = map {my($k, $v)=/^(.*?)=(.*)/; $v=[ $v=~/{(.+?)}/g ] if $v=~/} +/; $k=>$v } grep {/=/} <DATA>;