Forum Moderators: coopster

Message Too Old, No Replies

Regex question

Please help!

         

andytwiz

7:33 pm on Feb 12, 2006 (gmt 0)

10+ Year Member



OK so docs tell me:

[^abc]+ matches any (nonempty) string which does not contain any of a, b and c (such as defg) i.e. the individual *characters*

All fair and good.

But how do i match any nonempty *string* with is not 'abc' (i.e. the whole word abc NOT just any of the individual characters)

Little_G

7:42 pm on Feb 12, 2006 (gmt 0)

10+ Year Member



Hi,
If I understand what you want to do properly then you don't really need regular expressions,
Try:

<?php
$string = "def";
if(!empty($string)){
if(strpos($string,"abc") === false){
return true;
}
else{
return false;
}
}
?>

Andrew

andytwiz

7:48 pm on Feb 12, 2006 (gmt 0)

10+ Year Member



Hi

Thanks. I know i dont need regular expressions and your solution would work, but thats actually part of a complicated regex that all works, except that part!

Any regex experts out there?

Cheers

Little_G

8:12 pm on Feb 12, 2006 (gmt 0)

10+ Year Member



Hi,

Try this:


<?php
$string = "dkjsdglkrfngabcjdjfgd";
$match_length = preg_match("&!(abc)&", $string, $matches);
var_dump($matches);
?>

Andrew

andytwiz

3:51 pm on Feb 13, 2006 (gmt 0)

10+ Year Member



Thanks but that seems to give an empty array using your test data...

What I'm actually trying to do is remove everthing in a page of html from <html> to the first <table

I know this can be done in PHP but this is just the start of the replacement I need to create so I'd like to do it in regex as its easier to build on.

in terms of your example:

$string = "dkjsdglkrfBngABCjdjfgd";
$match_length = preg_match("/glk[^ABC]*/", $string, $matches);
var_dump($matches);
exit();

should return 'glkrfBng' but it actually returns 'glkrf' because it stops on the first B not the whole ABC

Cheers

Little_G

5:09 pm on Feb 13, 2006 (gmt 0)

10+ Year Member



Hi,

Okay, try this:


<?php
header("Content-Type: text/plain");
$string = "<html><head><title>Cool WebSite</title></head><body><table><tr>etc.";
$string = preg_replace("$<html>(.*)<table>$", "<html><table>", $string);
var_dump($string);
?>

Andrew