views:

304

answers:

8

I am trying to figure out a regular expression which matches any string with 8 symbols, which doesn't equal "00000000".

can any one help me?

thanks

+12  A: 

In at least perl regexp using a negative lookahead assertion: ^(?!0{8}).{8}$, but personally i'd rather write it like so:

length $_ == 8 and $_ ne '00000000'

Also note that if you do use the regexp, depending on the language you might need a flag to make the dot match newlines as well, if you want that. In perl, that's the /s flag, for "single-line mode".

Christoffer Hammarström
Christoffer - use @Marcelo and @klausbyskov to notify both about your changes ... this way they will get a real notification in their status bar and hopefully return to your answer again :) See http://meta.stackoverflow.com/questions/4798/lets-make-it-easier-to-reply-within-comments
tanascius
Ah, thanks @tanascius, @Marcelo, and @klausbyskov. :)
Christoffer Hammarström
+8  A: 

Unless you are being forced into it for some reason, this is not a regex problem. Just use len(s) == 8 && s != "00000000" or whatever your language uses to compare strings and lengths.

Marcelo Cantos
and of course the size check.
Nix
I am using Enterprise Library Validation, where I need to add regular expression, thats I want to know how can I figure out regex.
Wilson
ndim
True, and if this is a homework assignment - it needs to be specified. +1
M.A. Hanin
A: 

Unless you have unspecified requirements, you really don't need a regular expression for this:

if len(myString) == 8 and myString != "00000000":
    ...

(in the language of your choice, of course!)

RichieHindle
@Ritchie, what if the string has to be extracted from a larger string...
James
@James: The question says "any string with 8 symbols". But the question seriously underspecifies the problem, so who knows?
RichieHindle
A: 

If you need to extract all eight character strings not equal to "000000000" from a larger string, you could use

"(?=.{8})(?!0{8})."

to identify the first character of each sequence and extract eight characters starting with its index.

Jens
That would only extract one character. Try `(?!0{8}).{8}`.
Christoffer Hammarström
@Christoffer: That was my intention. I thought maybe the OP wanted to get every eight characters matching his requirements from a larger string. Like `12345678` and `23456789` from `123456789`. I do not think this can be achieved with Regex only, but identifying the first character helps.
Jens
A: 

Wouldn't ([1-9]*|\D){8} do it? Or am I missing something here (which is actually just the inverse of ndim's, which seems like it oughta work).

I am assuming the characters was chosen to include more than digits.

Ok so that was wrong, so Professor Bolo did I get a passing grade? (I love reg expressions so I am really curious).

>>> if re.match(r"(?:[^0]{8}?|[^0]{7}?|[^0]{6}?|[^0]{5}?|[^0]{4}?|[^0]{3}?|[^0]2}?|[^0]{1}?)", '00000000'):
    print 'match'
... 
>>> if re.match(r"(?:[^0]{8}?|[^0]{7}?|[^0]{6}?|[^0]{5}?|[^0]{4}?|[^0]{3}?|[^0]{2}?|[^0]{1}?)", '10000000'):
...     print 'match'
match
>>> if re.match(r"(?:[^0]{8}?|[^0]{7}?|[^0]{6}?|[^0]{5}?|[^0]{4}?|[^0]{3}?|[^0]{2}?|[^0]{1}?)", '10011100'):
...     print 'match'
match
>>> 

That work?

zenWeasel
how about 10000000?
rmarimon
Right, isn't it to match any string that is *not* 00000000? Which 100000000 is not and is picked up by my regex. Or correct me.
zenWeasel
First of all, your regexp doesn't match e.g. 11101111. Second of all, due to the asterisk the regexp matches strings of any length.
Bolo
@zenWeasel It's still wrong, since it matches 123 and 123456789. Only 8-character long strings should be matched (excluding 00000000).
Bolo
Ah well. Thanks for checking my work anyway. Good exercise. I'll get it yet. :)
zenWeasel
Not sure why I deserved a down vote for making a determined effort to answer a question to which I still see no accepted response. Everybody picks on the weasel.
zenWeasel
+1  A: 

As mentioned in the other answers, regular expressions are not the right tool for this task. I suspect it is a homework, thus I'll only hint a solution, instead of stating it explicitly.

The regexp "any 8 symbols except 00000000" may be broken down as a sum of eight regexps in the form "8 symbols with non-zero symbol on the i-th position". Try to write down such an expression and then combine them into one using alternative ("|").

Bolo
+1 for picking up the smell of homework. ;)
Pretzel
A: 

Of course, one would simply check

if stuff != '00000000'
   ...

but for the record, one could easily employ heavyweight regex (in Perl) for that ;-)

...
use re 'eval';

my @strings = qw'00000000 00A00000 10000000 000000001 010000';
my $L = 8;

print map "$_ - ok\n",
      grep /^(.{$L})$(??{$^Nne'0'x$L?'':'^$'})/,
      @strings;

...

prints

00A00000 - ok
10000000 - ok

go figure ;-)

Regards

rbo

rubber boots
A: 

If you need a regex, ^(?!0{8})[A-Za-z0-9]{8}$ will match a string of exactly 8 characters. Changing the values inside the [] will allow you to set the accepted characters.

Stevo3000