Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Ignoring certain regex matches

     

lindajames

6:09 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



Hi,

I have the following code that checks to see if the word error (not case sensative) exists and if so it displays an error message if not then it continues with executing the rest of script

if ($response->is_success && $Content!~ /error/i) {
return $response->content;
}
else {
$Error_msg = $response->message;
return undef;
}
}

the problem is that on the page that it looks at there is always a fixed word there called "Error Pages" so i wanted the above code to do exactly what it does but ignore the exact word "Error Pages"

can anyone help me with this?

Cheers
Linda

sugarkane

7:32 pm on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change your regex to:

/(error.*?){2,}/gi

and it should work.

Another way is a bit more of a kludge, but may be useful in a wider context (eg you don't know how many times 'Error Pages' appears in the document, but wanted to ignore them all) would be to do a regex substitution on 'Error Pages' before and after the test so that it no longer matches, eg:

$content=~s/Error Pages/Er1234567ror Pages/g;
# Do your test here
$content=~s/Er1234567ror Pages/Error Pages/g;

lindajames

9:09 pm on Jan 30, 2004 (gmt 0)

10+ Year Member



sugarkane mentioned that i can use the following:

$content=~s/Error Pages/Er1234567ror Pages/g;
if ($response->is_success && $Content!~ /error/i) {
$content=~s/Er1234567ror Pages/Error Pages/g;
# carry on with whatever you're doing if 'error' is found
}
else {
$content=~s/Er1234567ror Pages/Error Pages/g;
# carry on with whatever you're doing if 'error' is NOT found
}

but the problem is i cannot get it to work with my code. my code looks as follows, can anyone please help me get the above code to work with the code below where it reads if ($response->is_success && :

my $request = GET $query_string;

$request->authorization_basic($User_name, $Password);

my $response = $ua->request($request);
$Content = $response->content;

if ($response->is_success && $Content!~ /error (?!pages)/i) {
return $response->content;
}
else {
$Error_msg = $response->message;
return undef;
}
}

sugarkane

12:28 am on Feb 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



my $request = GET $query_string;

$request->authorization_basic($User_name, $Password);

my $response = $ua->request($request);
$Content = $response->content;

$content=~s/Error Pages/Er1234567ror Pages/g;
if ($response->is_success && $Content!~ /error/i) {
$content=~s/Er1234567ror Pages/Error Pages/g;
return $response->content;
}
else {
$Error_msg = $response->message;
return undef;
}
}

As I mentioned earlier it's a bit of a kludge, but should hopefully sort out the problem in your particular case.

lindajames

12:52 am on Feb 1, 2004 (gmt 0)

10+ Year Member



thanx, i'll try it out

dkubb

4:07 am on Feb 1, 2004 (gmt 0)

10+ Year Member



The following should work without any kludges. It uses a negative look-ahead assertion to look for the exact word "error" that is not followed by the word "pages".

Also note that some of the other examples in this thread would mistakenly match words that have error as a substring, like "terror" or "errors". You can account for this by using \b to match the word boundaries.

if($response->is_success and $response->content !~ /\berror\b(?!\s+pages)/i) {
return $response->content;
}
else {
$Error_msg = $response->message;
return undef;
}

lindajames

8:52 pm on Feb 1, 2004 (gmt 0)

10+ Year Member



thanx dkubb,

it worked. can you explain what exactly the line below does:

if($response->is_success and $response->content!~ /\berror\b(?!\s+pages)/i) {

cheers

 

Featured Threads

Hot Threads This Week

Hot Threads This Month