It doesn't do what you are thinking. Not that I can figure out what you are thinking quite, but I'm quite sure it doesn't do it.

For a start rrv193\d{4}cm => "RNF191", is a syntax error. Possibly you want 'rrv193\d{4}cm' => "RNF191", which at least compiles, but my guess is that you are heading at the problem from the wrong direction. I suspect you are hoping that the hash key in some magical fashion will behave as a regular expression and will "match" any string provided as a "key" that it matches on. Sorry, it don't work that way.

So, what are you really trying to do. Not the solution you've come up with, but the bigger problem you are trying to solve?

My guess is you have a number of these matches and you want to provide a different replacement string for each one. So lets run with that idea for a moment. Consider:

use strict; use warnings; my %Ren2Dol = ( 'rrv193\d{4}cm' => "RNF191", 'rrv194\d{4}cm' => "RNF196", 'rrv195\d{4}cm' => "RNF200", ); while (<DATA>) { chomp; my ($base, $rest) = split /[\.\_]/, $_, 2; my ($match) = grep {$base =~ /$_/} keys %Ren2Dol; if (defined $match) { print "$Ren2Dol{$match}\n"; } else { print "$base\n"; } } __DATA__ rrv1931009cm.new090626 rrv1941009cm.new090626 rrv1951009cm.new090626 rrv1961009cm.new090626 rrv1931234cm.new090626

Prints:

RNF191 RNF196 RNF200 rrv1961009cm RNF191

True laziness is hard work

In reply to Re: regular expression in a hash by GrandFather
in thread regular expression in a hash by lamasculo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.