in reply to Determining the regex for foo=bar

Neglecting the duplicate key issue so astutely pointed out by BrowserUK, I assume your lack of function is because you are capturing only 'word' characters, but your second key contains a space. Crafting regular expressions without a complete basis set is problematic at best, but perhaps you should consider using the $ metacharacter to anchor the second capture to the end of the line?

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %filehash; while (<DATA>) { if ( /(\w*) = (.+$)/ ) { $filehash{ $1 } = $2; } } print Dumper(\%filehash); __DATA__ foo1 = bar foo2 = bar bar

See perlre or perlretut for more info.

Replies are listed 'Best First'.
Re^2: Determining the regex for foo=bar
by ack (Deacon) on Mar 12, 2010 at 02:20 UTC

    You'e solution is the one I came up with...almost.

    My regex was /(\w+} = (.+)$/ where I chose not to include the end-of-line in the second capturing expression.

    Generally I prefer the regex approach to the split() approach because of the possibility that there might be extraneous info prior to the word to be used as the key.

    But of course, if anything (and everything) before the = sign is to be used as the 'key' then either the split() or an adjusted regex /^(,+) = (.+)$/ would work fine for me. But then I'm not sure how the OP would only capture, in the hash, those keys that he's interested in (which seem to be simple words). Of course, I guess that would'nt be a problem if the only thing before the ='s was always just a single-word key.

    ack Albuquerque, NM