匹配除“00000000”之外的所有 8 个字符串的正则表达式

发布于 2024-08-29 11:23:41 字数 86 浏览 4 评论 0原文

我试图找出一个正则表达式,它与任何具有 8 个符号的字符串相匹配,该符号不等于“00000000”。

有人可以帮助我吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

︶葆Ⅱㄣ 2024-09-05 11:23:41

至少在 Perl 正则表达式中使用否定先行断言:^(?!0{8}).{8}$,但我个人宁愿这样写:

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

另请注意,如果您确实使用正则表达式,根据语言的不同,您可能需要一个标志来使点也匹配换行符(如果您愿意的话)。在 Perl 中,这是 /s 标志,表示“单行模式”。

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".

皓月长歌 2024-09-05 11:23:41

除非您出于某种原因被迫这样做,否则这不是正则表达式问题。只需使用 len(s) == 8 && s != "00000000" 或您的语言用来比较字符串和长度的任何内容。

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.

最冷一天 2024-09-05 11:23:41

如果您需要正则表达式,^(?!0{8})[A-Za-z0-9]{8}$ 将匹配恰好包含 8 个字符的字符串。更改 [] 内的值将允许您设置接受的字符。

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.

不甘平庸 2024-09-05 11:23:41

正如其他答案中提到的,正则表达式不是完成此任务的正确工具。我怀疑这是一项作业,因此我只会暗示一个解决方案,而不是明确说明。

正则表达式“除了 00000000 之外的任何 8 个符号”可以分解为八个正则表达式的和,形式为“第 i 个位置上有非零符号的 8 个符号”。尝试写下这样一个表达式,然后使用替代项(“|”)将它们组合成一个。

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 ("|").

只涨不跌 2024-09-05 11:23:41

除非您有未指定的要求,否则您实际上不需要为此使用正则表达式:(

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

当然,用您选择的语言!)

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!)

草莓味的萝莉 2024-09-05 11:23:41

如果您需要从较大的字符串中提取不等于“000000000”的所有八个字符串,您可以使用

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

标识每个序列的第一个字符并提取从其索引开始的八个字符。

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.

巴黎夜雨 2024-09-05 11:23:41

当然,人们会简单地检查一下

if stuff != '00000000'
   ...

,但为了记录在案,人们可​​以轻松地使用
重量级正则表达式(Perl 中);-)

...
use re 'eval';

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

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

打印

00A00000 - ok
10000000 - ok

gofigure ;-)

问候

rbo

})/, @strings; ...

打印

gofigure ;-)

问候

rbo

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?'':'^

prints

00A00000 - ok
10000000 - ok

go figure ;-)

Regards

rbo

})/, @strings; ...

prints

go figure ;-)

Regards

rbo

宁愿没拥抱 2024-09-05 11:23:41

([1-9]**|\D*){8} 不会这样做吗?或者我在这里遗漏了一些东西(这实际上只是 ndim 的反面,这似乎应该有效)。

我假设选择的字符包含的数字以上。

好吧,那是错误的,所以博洛教授我的成绩合格了吗? (我喜欢reg表达式所以我真的很好奇)。

>>> 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
>>> 

那个工作?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文