avi_india
asked on
One more simple perl + regular expression question
In continuation from https://www.experts-exchange.com/questions/22073185/simple-perl-regular-expression-question.html
using the solution given in above question, I get following
/cgi-bin/xmlsearch?strict= 1&encoding =iso-8859- 1&query=3A 19341&type =adv&hits= 50&offset= 750&sortsp ec=-mtime
Now, I want to replace
hits=50 with hits=500
and
offset=750 with offset=0
How do I do that?
using the solution given in above question, I get following
/cgi-bin/xmlsearch?strict=
Now, I want to replace
hits=50 with hits=500
and
offset=750 with offset=0
How do I do that?
If you only want to replace the examplers above, try:
open( FILE, "< $filename" ) or die "Can't open $filename : $!";
while( <FILE> ) {
($query) =~ /GET (.*) HTTP/;
$query =~ s/hits=50/hits=500/;
$query =~ s/offset=50/hits=500/;
print "$query\n";
}
close FILE;
If you want to replace any number, try:
open( FILE, "< $filename" ) or die "Can't open $filename : $!";
while( <FILE> ) {
($query) =~ /GET (.*) HTTP/;
$query =~ s/hits=\d+/hits=500/;
$query =~ s/offset=\d+/offset=0/;
print "$query\n";
}
close FILE;
Cheers Xanius
open( FILE, "< $filename" ) or die "Can't open $filename : $!";
while( <FILE> ) {
($query) =~ /GET (.*) HTTP/;
$query =~ s/hits=50/hits=500/;
$query =~ s/offset=50/hits=500/;
print "$query\n";
}
close FILE;
If you want to replace any number, try:
open( FILE, "< $filename" ) or die "Can't open $filename : $!";
while( <FILE> ) {
($query) =~ /GET (.*) HTTP/;
$query =~ s/hits=\d+/hits=500/;
$query =~ s/offset=\d+/offset=0/;
print "$query\n";
}
close FILE;
Cheers Xanius
ASKER
how can we make it more precise? I mean consider & also while defining this expression?
if you want to replace all of but only the explicit figures above:
open( FILE, "< $filename" ) or die "Can't open $filename : $!";
while( <FILE> ) {
($query) =~ /GET (.*) HTTP/;
$query =~ s/hits=(0|1|50|567|2341)\& /hits=500\ &/;
$query =~ s/offset=\d+/offset=0/;
print "$query\n";
}
close FILE;
open( FILE, "< $filename" ) or die "Can't open $filename : $!";
while( <FILE> ) {
($query) =~ /GET (.*) HTTP/;
$query =~ s/hits=(0|1|50|567|2341)\&
$query =~ s/offset=\d+/offset=0/;
print "$query\n";
}
close FILE;
ASKER
Hey Xanius
Thanks.
Actually, what I meant by giving those examples was that it could be any number. But as its a url, u might have similar patter elsewhere also..
like sdf34r32rf*Q$TR#@RGFERhits =324234r!@ &wqrefjki
I want to change the instances where hits=<some no> is enclosed between "&"
Thanks.
Actually, what I meant by giving those examples was that it could be any number. But as its a url, u might have similar patter elsewhere also..
like sdf34r32rf*Q$TR#@RGFERhits
I want to change the instances where hits=<some no> is enclosed between "&"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could put a & before the hits and offset...
$query =~ s/\&hits=\d+\&/\&hits=500\ &/;
$query =~ s/\&offset=\d+\&/\&offset= 0\&/;
$query =~ s/\&hits=\d+\&/\&hits=500\
$query =~ s/\&offset=\d+\&/\&offset=
Hi avi_india,
> Actually, what I meant by giving those examples was that it could be any number. But as its a url, u might have similar patter elsewhere also..
>like sdf34r32rf*Q$TR#@RGFERhits =324234r!@ &wqrefjki
I probably misunderstood your last question; Adam314 is right, but ten it should be necessary to have the \& only up front to allow
$query =~ s/\&hits=\d+/\&hits=500/;
$query =~ s/\&offset=\d+/\&offset=0/ ;
Xanius
> Actually, what I meant by giving those examples was that it could be any number. But as its a url, u might have similar patter elsewhere also..
>like sdf34r32rf*Q$TR#@RGFERhits
I probably misunderstood your last question; Adam314 is right, but ten it should be necessary to have the \& only up front to allow
$query =~ s/\&hits=\d+/\&hits=500/;
$query =~ s/\&offset=\d+/\&offset=0/
Xanius
> Actually, what I meant by giving those examples was that it could be any number. But as its a url, u might have similar patter elsewhere also..
> like sdf34r32rf*Q$TR#@RGFERhits =324234r!@ &wqrefjki
> I want to change the instances where hits=<some no> is enclosed between "&"
Do you mean that the above example string should be altered to sdf34r32rf*Q$TR#@RGFER&hit s=324234&r !@&wqrefjk i?
If so, try something like:
#!/usr/bin/perl -w
use strict;
my $str = 'sdf34r32rf*Q$TR#@RGFERhit s=324234r! @&wqrefjki ';
$str =~ s/(hits\s*=\s*\d+)/&$1&/;
print $str;
> like sdf34r32rf*Q$TR#@RGFERhits
> I want to change the instances where hits=<some no> is enclosed between "&"
Do you mean that the above example string should be altered to sdf34r32rf*Q$TR#@RGFER&hit
If so, try something like:
#!/usr/bin/perl -w
use strict;
my $str = 'sdf34r32rf*Q$TR#@RGFERhit
$str =~ s/(hits\s*=\s*\d+)/&$1&/;
print $str;
ASKER
hits=50
hits=1
hits=567
hits=0
hits=2341
similarly for offset