Perl hash substitution with special characters in keys

前端 未结 1 1817
花落未央
花落未央 2021-01-29 08:21

My current script will take an expression, ex:

my $expression = \'( a || b || c )\';

and go through each boolean combination of inputs using su

1条回答
  •  后悔当初
    2021-01-29 08:49

    Problem 1

    You use the pattern a* thinking it will match only a*, but a* means "0 or more a". You can use quotemeta to convert text into a regex pattern that matches that text.

    Replace

    my $keys = join '|', keys %stimhash;
    

    with

    my $keys = join '|', map quotemeta, keys %stimhash;
    

    Problem 2

    \b
    

    is basically

    (?

    But * (like the space) isn't a word character. The solution might be to replace

    s/($keys)\b/$stimhash{$1}/g
    

    with

    s/($keys)(?![\w*])/$stimhash{$1}/g
    

    though the following make more sense to me

    s/(?

    Personally, I'd use

    s{([\w*]+)}{ $stimhash{$1} // $1 }eg
    

    0 讨论(0)
提交回复
热议问题