Page 1 of 1

help with a simple regular expression

Posted: Thu Mar 23, 2006 4:10 am
by jasongr
Hi

I am trying to write a simple regular expression that will help me lookup
all the occurrences of & followed by a-z or A-Z with the following exception;
  (case insensitive) should not be matched

so I will be able to find:
&aad
&De

but not:
&&
 
&NBSp;

any help would be appreciated

Posted: Thu Mar 23, 2006 8:54 am
by mattcooper
This'll help you along...

Code: Select all

$exclude="&nbsp";
$text="&NBSP";
if(eregi($exclude, $text)){ // compare the variables and check for a case-insensetive match
echo "Match";
}
else{
echo "No match";
}
Run it and see what happens; hopefully you'll be able to extrapolate the rest of what you need from there!

Hope this helps.

Posted: Thu Mar 23, 2006 9:10 am
by pickle
Moved to Regex forum.

Try this:

Code: Select all

'/&(?!nbsp)[a-zA-Z]+/i'
That's a pattern to use with preg_match(), not ereg()

Also, take a look at ~ d11wtq's Regex Advanced Tutorial II. You might find some useful stuff in there.

Posted: Thu Mar 23, 2006 9:50 am
by mattcooper
pickle wrote:a pattern to use with preg_match(), not ereg()
Am I to take it that, in this instance, I was grossly incorrect?!

Posted: Thu Mar 23, 2006 9:53 am
by pickle
Not at all. preg_match() uses the PCRE and is often faster than eregi(). That also means the patterns will look different.

I was just saying that because I saw you're example used eregi() and I didn't want him trying to use my pattern in eregi() (as it wouldn't ever work).

Posted: Thu Mar 23, 2006 10:10 am
by mattcooper
Excellent news! I often find myself wanting to further my own understanding of PHP by taking on other developers' problems and have come a-cropper a few times and been, in a manner of speaking, "pulled-up" on it.

It's good to know that I'm not leading someone astray in this instance! Thanks fella...