in reply to Replacing parts of a string

Use CGI.pm to do it for you thusly (this is very robust):

$str = "calendarview.pl?loginid=102138&month=04&year=2003&student_id=& +user_type=TUTOR&CalendarName=102138Academic&framename=top.index_main& +session_number=618280744437303"; # first amputate the calendarview.pl? bit $str =~ s/^[^\?]+\?//; # use CGI.pm to parse the string by creating a CGI instance from it use CGI; $q = CGI->new($str); # modify whatever you want, lets delete what you specified $q->delete('CalendarName'); $q->delete('month'); $q->delete('year'); # and modify loginid just for laughs $q->param( 'loginid', 'haxor' ); # add a new param if you want $q->param('i am a new one', 'this is my value' ); # write out the new query string (default is to use ; not & so we do:) $CGI::USE_PARAM_SEMICOLONS = 0; print $q->query_string(); __DATA__ loginid=haxor&student_id=&user_type=TUTOR&framename=top.index_main&ses +sion_number=618280744437303&i%20am%20a%20new%20one=this%20is%20my%20v +alue

You will be hard pressed to break this solution - it is much better than using regexes from a robustness point of view.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Replacing parts of a string
by spiderbo (Sexton) on Apr 11, 2003 at 14:58 UTC
    You're Good! That looks a smart plan indeed. Will give it a go.
Re: Re: Replacing parts of a string
by spiderbo (Sexton) on Apr 15, 2003 at 08:23 UTC

    silly question, however I know it will take you seconds to answer and me hours to find, but, how do you get the bit which you strippped when you did:

    $str =~ s/^[^\?]+\?//;

    i.e. I am looking for 'calendarview.pl'

    Thank you.

      You can capture into $1, $2, $etc using parens around (what you want) in a regex. If there are multiple parens then you get the first capture in $1, next in $2, etc

      $str = 'http://somesite.com/cgi-bin/calendar.pl?some=qstring'; # the typical perlish idiom looks like this # we are capturing $1 and $2 and assigning them to vars # all in one line (note this uses m//) my ( $site, $q_string ) = $str =~ m/^([^\?]+)\?(.*)$/; print "site: $site\nq string: $q_string\n"; # simple way, just modifying the s/// we had $str =~ s/^([^\?]+)\?//; my $capture = $1; print $capture; # $str now contains q string

      Details see perlre

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print