Re: Replacing parts of a string
by tachyon (Chancellor) on Apr 11, 2003 at 14:51 UTC
|
$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
| [reply] [d/l] |
|
You're Good! That looks a smart plan indeed. Will give it a go.
| [reply] |
|
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.
| [reply] [d/l] |
|
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
| [reply] [d/l] |
Re: Replacing parts of a string
by krujos (Curate) on Apr 11, 2003 at 14:00 UTC
|
If you always have numeric followed by alphabetic in that field how about:
s/(CalandarName=)\d+\w+(&)/$1$ReplacementString$2/
cheers,
Josh
UPDATE: Fixed formatting | [reply] [d/l] |
|
| [reply] [d/l] |
Re: Replacing parts of a string
by jasonk (Parson) on Apr 11, 2003 at 14:08 UTC
|
You could do the regexp thing: $string =~ s/month=\d+/month=$newmonth/g;, or if the code you are working on is calendarview.pl, and you already have a CGI.pm object you are working with, I find this is a really easy way to get slightly modified URLs:
my $cgi = new CGI;
# .... bunch of calendarview.pl code here ....
# need to get a url that takes us back to new years
my $newyears = new CGI($cgi);
$newyears->param('month',1);
$newyears->param('day',1);
my $newyears_url = $newyears->self_url;
Generally I just abstract this into a little subroutine:
sub new_self_url {
my %params = @_;
my $new = new CGI($cgi);
foreach(keys %params) {
$new->param($_,$params{$_});
}
return $new->self_url;
}
# now you can get urls back to your script with different
# dates like this:
my $newyears = new_self_url(month => 1, day => 1);
my $christmas = new_self_url(month => 12, day => 25);
We're not surrounded, we're in a target-rich environment! |
---|
| [reply] [d/l] [select] |
|
$string =~ s/month=\d+/month=$newmonth/g; for my month and year.
Still to find a solution for the CalendarName then... In response to your comment, it could be numeric or alphnumeric, in any order.
| [reply] |
Re: Replacing parts of a string
by arturo (Vicar) on Apr 11, 2003 at 14:52 UTC
|
OK, this will not enhance your regex-fu, and it may not be the most efficient (in terms of runtime resource allocation) solution, but it's a dead easy one to use, and very efficient in terms of programmer-hours spent on the problem. Following up on jasonk's suggestion, you don't even need it to be the case that the script doing the parsing is calendarview.pl. Simply feed the query string to a CGI.pm object and let it do the parsing for you:
#!/usr/bin/perl
+
use CGI;
use strict;
use warnings;
# note you want everything after the ?
my $querystring='loginid=102138&month=04&year=2003&student_id=&user_ty
+pe=TUTOR&CalendarName=102138Academic&framename=top.index_main&session
+_number=618280744437303';
+
my $q = CGI->new($querystring);
+
foreach my $p ( $q->param() ) {
print "Parameter $p : " . $q->param($p) . "\n";
}
If not P, what? Q maybe? "Sidney Morgenbesser" | [reply] [d/l] |
|
ditto...use CGI.pm or similar--or be at the peril of code injection inside URLs. You should also use taint...and check (with a simple regex) that the parameters you receive are alphanumeric only (no quotes etc.)
Chris
| [reply] |
Re: Replacing parts of a string
by Tomte (Priest) on Apr 11, 2003 at 14:22 UTC
|
my $string = "calendarview.pl?loginid=102138&month=04&year=2003&studen
+t_id=&user_type=TUTOR&CalendarName=102138Academic&framename=top.index
+_main&session_number=618280744437303";
my $i = index($string,"CalendarName=") + length("CalendarName=");
my $j = index($string, "&", $i);
substr($string,$i,$j-$i) = "";
I don't recommend it, but I'm always astounded that this is possible, maybe one enlightened monk can come up with a suggestion where this lvalue-assignement (?!?) is useful.
Update: in Re: Replace zero-width grouping?, BrowserUk comes up with a good use of an substr lvalue--assignement.
regards,
tomte
Hlade's Law:
If you have a difficult task, give it to a lazy person --
they will find an easier way to do it.
| [reply] [d/l] |