Dynamic data within regex pattern?

Discussion in 'Perl Misc' started by DM, Oct 1, 2004.

  1. DM

    DM Guest

    Is there a way to set up a regex substitution so that the pattern is dynamically
    generated at runtime?

    I.e., something along the lines of...

    $output =~ s/$someVar/$someOtherVar/g;

    I read in my Perl book that the replacement string is variable interpolated, but
    it didn't say anything about that being the case for the regex pattern. So I
    guess in the situation above, the regex would match the end of a line followed
    by "someVar". And if you escaped the dollar sign, it would match "$someVar"
    litterally.

    Any assistance would be greatly appreciated.
    DM, Oct 1, 2004
    #1
    1. Advertising

  2. DM wrote:
    > Is there a way to set up a regex substitution so that the pattern
    > is dynamically generated at runtime?
    >
    > I.e., something along the lines of...
    >
    > $output =~ s/$someVar/$someOtherVar/g;


    What happened when you tried it?

    > I read in my Perl book that the replacement string is variable
    > interpolated, but it didn't say anything about that being the case
    > for the regex pattern. So I guess in the situation above, the regex
    > would match the end of a line followed by "someVar".


    You guessed wrong.

    > Any assistance would be greatly appreciated.


    You don't need any assistance at this stage. You need to play with
    Perl, and figure out what 'works' and what doesn't. And maybe read a
    little more.

    Come back here if you fail in solving a particular problem.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 1, 2004
    #2
    1. Advertising

  3. DM

    Paul Lalli Guest

    DM wrote:

    > Is there a way to set up a regex substitution so that the pattern is
    > dynamically generated at runtime?
    >
    > I.e., something along the lines of...
    >
    > $output =~ s/$someVar/$someOtherVar/g;
    >
    > I read in my Perl book that the replacement string is variable
    > interpolated, but it didn't say anything about that being the case for
    > the regex pattern.


    What Perl book was that, may I ask?

    > So I guess in the situation above, the regex would
    > match the end of a line followed by "someVar".


    Why just guess? Why not actually try it?

    > And if you escaped the
    > dollar sign, it would match "$someVar" litterally.
    >
    > Any assistance would be greatly appreciated.


    Read the section "Regexp Quote-Like Operators" from perldoc perlop.
    Specifically, the paragraph beginning "PATTERN may contain variables,"

    Paul Lalli
    Paul Lalli, Oct 1, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    1
    Views:
    931
    Ilias Lazaridis
    Sep 22, 2006
  2. Xah Lee
    Replies:
    8
    Views:
    458
    Ilias Lazaridis
    Sep 26, 2006
  3. Xah Lee
    Replies:
    2
    Views:
    213
    Xah Lee
    Sep 25, 2006
  4. Replies:
    2
    Views:
    384
  5. ChrisC
    Replies:
    4
    Views:
    161
    ChrisC
    Jun 25, 2010
Loading...

Share This Page