Solved

One more simple perl + regular expression question

Posted on 2006-11-27
11
168 Views
Last Modified: 2010-03-05
In continuation from http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_22073185.html

using the solution given in above question, I get following
/cgi-bin/xmlsearch?strict=1&encoding=iso-8859-1&query=3A19341&type=adv&hits=50&offset=750&sortspec=-mtime

Now, I want to replace
hits=50 with hits=500
and
offset=750 with offset=0

How do I do that?

0
Comment
Question by:avi_india
11 Comments
 

Author Comment

by:avi_india
ID: 18018888
in the original string it could be

hits=50
hits=1
hits=567
hits=0
hits=2341


similarly for offset
0
 
LVL 10

Expert Comment

by:xanius
ID: 18019258
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
0
 

Author Comment

by:avi_india
ID: 18020367
how can we make it more precise? I mean consider & also while defining this  expression?
0
 
LVL 10

Expert Comment

by:xanius
ID: 18021684
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;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:avi_india
ID: 18025312
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 "&"

0
 
LVL 10

Accepted Solution

by:
xanius earned 50 total points
ID: 18026677
Ok. so for any number between  "=" and "&", the following regex would work:

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;

or as in your last example (althogh i doubt a hit figure of '324234r!@&wqrefjki'):

open( FILE, "< $filename" ) or die "Can't open $filename : $!";

while( <FILE> ) {
    ($query) =~ /GET (.*) HTTP/;
    $query =~ s/hits=(.+?)\&/hits=500\&/;
    $query =~ s/offset=(.+?)\&/offset=0\&/;
    print "$query\n";
}
close FILE;
0
 
LVL 39

Expert Comment

by:Adam314
ID: 18028586
You could put a & before the hits and offset...

$query =~ s/\&hits=\d+\&/\&hits=500\&/;
$query =~ s/\&offset=\d+\&/\&offset=0\&/;

0
 
LVL 10

Expert Comment

by:xanius
ID: 18028893
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
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 18059471
> 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&hits=324234&r!@&wqrefjki?

If so, try something like:

#!/usr/bin/perl -w
use strict;

my $str = 'sdf34r32rf*Q$TR#@RGFERhits=324234r!@&wqrefjki';

$str =~ s/(hits\s*=\s*\d+)/&$1&/;

print $str;

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now