s/$pattern_to_match/$pattern_to_replace/ -- how can i use matched expressions?

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default s/$pattern_to_match/$pattern_to_replace/ -- how can i use matched expressions?

    Hi all,

    I want to use the substitution operator with variables rather than
    string literals -- no problem, right?

    My problem comes when I try to use the $1, $2, etc. variables to refer
    to the matched expressions.
    For example:

    $pattern_to_match = 'I am (.*)';
    $pattern_to_replace = 'You are $1';
    $line =~ s/$pattern_to_match/$pattern_to_replace/g;

    For example, if $line contains 'I am green', I want it to be replace
    with 'You are green'.
    However, the code fragment above would result in 'You are ' -- the $1
    variable doesn't work as the match substitution...

    Can anyone help me out?
    I realize I may be able to work something out with the s///e option,
    but this looks a little too complex for what I want to do.

    TIA
    Mithras Guest

  2. Similar Questions and Discussions

    1. #38860 [Fbk->Opn]: preg_match returns wrong positions of matched substrings when UTF-8 is used
      ID: 38860 User updated by: me at andr dot biz Reported By: me at andr dot biz -Status: Feedback +Status: ...
    2. DRWM8: How to remove underline from Matched text?
      How to remove underline from Matched text in search result? Is there any extension to do so? Thanx a lot PEPP
    3. Keeping Grid Checkbox Control matched with correct row
      Greetings, I have an issue that I am not seeing a blatant solution for. My DataGrid is an "Accept/Reject" type grid. So there is a check box in...
    4. How do I<STDIN> to a stack until a pattern is matched, How do I
      Is there an easy way to read <STDIN> into a stack until some pattern is matched and then break. I tried all sorts of (error producing) code, but...
    5. How do I<STDIN> to a stack until a pattern is matched, How do I
      > How can I do this? You can do it like this... # tested my @stack; while (<STDIN>) { last if /QUIT/; push @stack, $_;
  3. #2

    Default Re: s/$pattern_to_match/$pattern_to_replace/ -- how can i use matched expressions?

    [email]mybulkmail@myrealbox.com[/email] (Mithras) wrote:

    : I want to use the substitution operator with variables rather than
    : string literals -- no problem, right?
    :
    : My problem comes when I try to use the $1, $2, etc. variables to refer
    : to the matched expressions.
    : For example:
    :
    : $pattern_to_match = 'I am (.*)';
    : $pattern_to_replace = 'You are $1';

    Don't let yourself call the replacement string a "pattern."
    Badly named variables will only bring confusion.

    : $line =~ s/$pattern_to_match/$pattern_to_replace/g;
    :
    : For example, if $line contains 'I am green', I want it to be replace
    : with 'You are green'.
    : However, the code fragment above would result in 'You are ' -- the $1
    : variable doesn't work as the match substitution...
    :
    : Can anyone help me out?
    : I realize I may be able to work something out with the s///e option,
    : but this looks a little too complex for what I want to do.

    Not all that complex.

    my $pattern_to_match = 'I am (.*)';
    my $string_to_replace = '"You are $1"';
    my $line = 'I am green';
    $line =~ s/$pattern_to_match/$string_to_replace/eeg;
    print $line;

    Jay Tilton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139