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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegExp - Negation confusion- won't work my way. 1

Status
Not open for further replies.

MoGryph

Programmer
Nov 1, 2000
99
0
0
US
I'm trying to create a regular expression and having a ton of problems understanding how to get Negation to work correctly the way I need it to.
I'm trying to do this using regular expressions in Javascript, but the format should be the same

Here's a couple of samples of input:
1) openview
2) openview&lines=3
3) openview&count=30
4) openview&count=30&lines=3
5) openview&lines=3&count=30

I want to find only expressions which contain "openview", but do NOT contain "count"
I've tried
/openview[.]*^(count).*/i
/openview[.]*[^(count)].*/i
/openview[.]*[^(count)]*/i

and a few other strange obscure mixes and matches, but none of the them seem to work.
Can someone show me what the correct RegExp would be, and explain why, if it isn't apparent?

Thanks
 
What you are describing is a lookahead assertion and perl's regexs have them. I don't know if js does.

The two simplest lookaheads are (?=pattern) and (?!pattern). The first is true if the pattern does follow and the second if it doesn't. Neither use moves the "pointer" forward so you will continue to match from where you left off.

This code
Code:
   #!/usr/bin/perl

   my @data = qw{
   openview
   openview&lines=3
   openview&count=30
   openview&count=30&lines=3
   openview&lines=3&count=30
   };

   foreach (@data) {
        print "$_: ";
        print /openview(?!.*count)/ ? 'yes' : 'no';
        print "\n";
   }
produces this output:
Code:
   openview: yes
   openview&lines=3: yes
   openview&count=30: no
   openview&count=30&lines=3: no
   openview&lines=3&count=30: no

If you don't have lookaheads, won't something like
Code:
   /openview/ && ! /count/
sort you?

Yours,


fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Awesome!

Yes, that worked perfect. JS apparently does accept the lookahead assertion.

I know I could have created 2 RegExp expressions, and tested it twice, or even done it by checking :
(expression.indexOf("openview") > -1) && (expression.indexOf("count") == -1)

but I sure that using a single Regular expression must be more effiecient.

Thanks a bunch!
 
Glad to help and thanks for the js nod.

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top