topical media & game development

talk show tell print

basic-regex-26-GlobalReplace.pl / pl



  #!/usr/bin/perl -w
  use strict;
  print "This example uses the global modifier, 'g'\n\n";
  my myTestString = "Star Training Company courses are great. Choose Star for your training needs.";
  my myOnceString = myTestString;
  my myGlobalString = myTestString;
  my myPattern = "Star";
  my myReplacementString = "Moon";
  myOnceString =~ s/myPattern/myReplacementString/;
  myGlobalString =~ s/myPattern/myReplacementString/g;
  print "The original string was 'myTestString'.\n\n";
  print "After a single replacement it became 'myOnceString'.\n\n";
  print "After global replacement it became 'myGlobalString'.\n\n";


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.