in reply to converting an arbitrary string into a hash based on a pattern

use strict; use Data::Dumper; my %hash; $_="ABCD----[this] fgab [that] BFTE-- [other] AB CD EF---- [foo]"; while ( /\G\s*([\w ]+)[\s-]+\[(\w+)\]/g ) { $hash{$1}=$2; } print Dumper (\%hash); #$VAR1 = { # 'fgab ' => 'that', # 'ABCD' => 'this', # 'BFTE' => 'other', # 'AB CD EF' => 'foo' # };


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: converting an arbitrary string into a hash based on a pattern
by Anonymous Monk on Mar 04, 2005 at 04:55 UTC

    Hi Holli,

    Let me know the meaning of \G.

      From "perldoc perlre"
      \G Match only at pos() (e.g. at the end-of-match position of prior m//g) [snip] The "\G" assertion can be used to chain global matches (using " +m//g"), as described in "Regexp Quote-Like Operators" in perlop. It is + also useful when writing "lex"-like scanners, when you have several +patterns that you want to match against consequent substrings of your st +ring, see the previous reference. The actual location where "\G" wil +l match can also be influenced by using "pos()" as an lvalue: see "pos" + in perlfunc. Currently "\G" is only fully supported when anchored +to the start of the pattern; while it is permitted to use it elsewhere +, as in "/(?<=\G..)./g", some such uses ("/.\G/g", for example) current +ly cause problems, and it is recommended that you avoid such usage for n +ow.