I'm not quite sure what you mean by "arbitrary string variable" and "unknown in advance", but maybe something like:

c:\@Work\Perl\monks>perl -wMstrict -le "chomp(my $string_to_find = <STDIN>); ;; my @array_to_search = qw(Foo whathiddenever Bar 12hidden456); ;; for my $s (@array_to_search) { print qq{'$s' has '$string_to_find'} if $s =~ $string_to_find; } " hidden 'whathiddenever' has 'hidden' '12hidden456' has 'hidden'

Update: In general, strings and, better yet, regex objects created by  qr// (see Regexp Quote-Like Operators in perlop) can be interpolated into and thus used to compose other regex objects:

c:\@Work\Perl\monks>perl -wMstrict -le "chomp(my $string_to_find = <STDIN>); ;; my @array_to_search = qw(Foo whathi$denever Bar 12hi$den456); ;; my $rx_search = qr{ (?<= \d) \Q$string_to_find\E (?= \d) }xms; ;; for my $s (@array_to_search) { print qq{'$s' has '$string_to_find'} if $s =~ $rx_search; } " hi$den '12hi$den456' has 'hi$den'
This illustrates both interpolation for composition and metaquoting with  \Q ... \E of an interpolated variable. See Quote and Quote-like Operators in perlop, perlre.


Give a man a fish:  <%-{-{-{-<


In reply to Re: Can a regular expression include an arbitrary string variable? by AnomalousMonk
in thread Can a regular expression include an arbitrary string variable? by anautismobserver

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.