Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp

Status
Not open for further replies.

mlowe9

Programmer
Apr 3, 2002
221
US
How do I write a regular expression to exclude multiple characters. For example, if I have a 3-digit string, and I want to match anything EXCEPT 'ZZZ', how do I write the regexp to do that?

I've tried every combination of [^ZZZ] or [^Z][^Z][^Z] or [^(ZZZ)], nothing seems to work. Thank anyone very much for helping me out.
 
unfortunately regex does not really support a good way to test if something does not exist. You were on the right track with this: [^Z][^Z][^Z]. Actually... that probably should have worked. What I usually do to check if something doesn't exist is invert the return value from the test method. The following tests will alert true if the combination of zzz does not exist, and will alert false if it does exist:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

var regex = /zzz/;

var a = "abczzzxyq";
var b = "abczzxyz";
var c = "zz";
var d = "zzz    zzz";

alert(a + ": " + [!]![/!]regex.test(a));
alert(b + ": " + [!]![/!]regex.test(b));
alert(c + ": " + [!]![/!]regex.test(c));
alert(d + ": " + [!]![/!]regex.test(d));

</script>
<style type="text/css"></style>
</head>
<body>

</body>
</html>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Unfortunately, the ZZZ is not really what I need to check. Let's say I need everything except 'ABC' instead. The problem with the [^A][^B][^C] is that I need to to match exactly ABC, and this is acting like an "OR" not an "AND".

I was wanting to keep this in the regular expression realm, but I guess I'll have to code around it. Thanks for your help.
 
Let's say I need everything except 'ABC' instead. The problem with the [^A][^B][^C] is that I need to to match exactly ABC, and this is acting like an "OR" not an "AND".

Isn't this what you originally asked for? I guess I'm not understanding your question.....

You want to match everything except 'ABC', but the problem is that it's not matching exactly 'ABC'?

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Easy, monksnake. My response was partially directed to you. You said [^Z]{3}, which is for checking for NOT 3 Zs. That's why I changed it to read 'ABC' instead. Would I use [^ABC] or [^A][^B][^C], neither of which worked when I tried them.

It's just checking each position, but I need it to check the group as a whole. I also tried [^(ABC)], but didn't work either.

I finally decided to just code around it, because it was easier than trying to figure out the regular expression. So thank you BOTH for your time and help.

Matt
 
I'll just put this, don't know if it'll be read. You can test for ABC, then put the ! not clause in front of the test for ABC, like so:

Code:
!(/^ABC$/.test(evalString))


[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top