maddfisherman has asked for the wisdom of the Perl Monks concerning the following question:

how do you get all the characters between two words for example
$string = "stuff, Hello the name of this site is perlmonks.org, goodby +e, stuff"
how could i asign all the characters between "Hello" and "goodbye" to $data so that
$data = " the name of this site is perlmonks.org, "

Replies are listed 'Best First'.
Re: finding data in a string
by blakem (Monsignor) on Aug 21, 2001 at 01:54 UTC
    That one is easy.... use a regex:
    #!/usr/bin/perl use strict my $string = "stuff, Hello the name of this site is perlmonks.org, goo +dbye, stuff"; my ($newstring) = ($string =~ /Hello(.*?)goodbye/); print "'$newstring'\n";

    -Blake

Re: finding data in a string
by dga (Hermit) on Aug 21, 2001 at 01:55 UTC
    $string = "stuff, Hello the name of this site is perlmonks.org, goodby +e, stuff"; ($data) = $string =~ /Hello(.*)goodbye/; print "$data\n";

    Update: Some people can type really fast :-)

Re: finding data in a string
by maddfisherman (Sexton) on Aug 21, 2001 at 02:25 UTC
    what if its like this
    $string = "stuff, Hello the name of this site is perlmonks.org, goodby +e./n stuff Hello my name is bob, goodbye."
    how could i make it so that it gets both strings between Hello and goodbye
    @newarray = (" the name of this site is perlmonks.org, ", " my name is + bob, ")
    does the /n return make a difference
      @newarray = $string =~ /Hello(.*?)goodbye/g
      I think I can tame this one... use the split function:

      my @newarray = split (/Hello/$string); 
      


      (man I hope that syntax is right... looked it up though, so it should be fine.)
      _________________________________________
      E-Bitch
      Tempora Mutantur Nos et Mutamur in Illis
      "The Times are Changed Even as We are Changed in Them"