Others have already pointed out some good ideas, but I'd like to expand on BrowserUk's point a bit more. The following is equivalent to his solution, but uses the builtin printf function to accomplish the job. (This way, you also get more control over what your output's going to look like, as a bonus.)
my $rx = qr"^DMSC0022\s+\S+\s+(\w+)$"; my $fmt = "Logfile_Connection_Lost Hostname: %s"; $_="DMSC0022 X10 oswald"; if(my @match = /$rx) { printf "$fmt\n", @match; }
If however you need true positional parameters, like if one of your output strings contains the parameters out of order as in Found $2 at $1, then that won't work for you. You need the following:
my $rx = qr"^DMSC0022\s+\S+\s+(\w+)$"; my $fmt = "Logfile_Connection_Lost Hostname: %1"; $_="DMSC0022 X10 oswald"; if(my @match = /$rx/) { (my $msg = $fmt) =~ s/%\d+/$match[$1]/e; print "$msg\n"; }

Note that you can't easily use literal % characters followed by digits in your output string formats then.

All the solutions in this thread which use qr and whatever else to avoid eval are much cleaner, more efficient, more robust, more secure and more maintainable. If you find yourself using eval "STRING"; you are either doing something very clever or something very stupid. And using it in clever and valid ways is difficult. For a relatively ordinary task, there's always a better device.

Makeshifts last the longest.


In reply to Re: Regex frustration by Aristotle
in thread Regex frustration by Anonymous Monk

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.