help with a simple regular expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

help with a simple regular expression

Post 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
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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?!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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...
Post Reply