Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Match a var in a regex

Expand Messages
  • Jean-Sebastien Guay
    Yes you can, and unless you want to replace each occurence of string with $var literally, do not escape the $ . For example : my $str = I am a blue
    Message 1 of 1 , May 27, 2003
    View Source
    • 0 Attachment
      Yes you can, and unless you want to replace each occurence of 'string' with '$var' literally, do not escape the '$'.

      For example :

      my $str = 'I am a blue butterfly';
      my $color = 'red';

      $str =~ s/blue/$color/;
      print $str; # prints 'I am a red butterfly'

      $str =~ s/$color/\$color/;
      print $str; # prints 'I am a $color butterfly'

      This also illustrates how you can do a completely user-controllable search-and-replace :

      my $str = 'What's up doc?';
      my $search = 'doc'; # In a real context, $search and $replace might
      my $replace = 'bob'; # be given by command-line or CGI script arguments

      $str =~ s/$search/$replace/g;

      See the perlre manpage for more details and other cool things you can do with regular expressions.

      Hope this helps!

      J-S

      ----- Original Message -----
      From: Joe Catanzaro
      To: perl-beginner@yahoogroups.com
      Sent: Tuesday, May 27, 2003 1:41 PM
      Subject: [PBML] Match a var in a regex


      Is it possible for me to do this?

      $lines = s/string/$var/;
      (obviously I would need to escape the $)

      If not, what's the best way for me to use a variable in a regex? Or rather,
      what's the best way to do a search and replace with a $var?

      Thanks,



      Joe Catanzaro
      joecatanzaro@...


      Yahoo! Groups Sponsor



      Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

      Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.