匹配除“00000000”之外的所有 8 个字符串的正则表达式
我试图找出一个正则表达式,它与任何具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
至少在 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".除非您出于某种原因被迫这样做,否则这不是正则表达式问题。只需使用 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.如果您需要正则表达式,
^(?!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.正如其他答案中提到的,正则表达式不是完成此任务的正确工具。我怀疑这是一项作业,因此我只会暗示一个解决方案,而不是明确说明。
正则表达式“除了 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 ("|").
除非您有未指定的要求,否则您实际上不需要为此使用正则表达式:(
当然,用您选择的语言!)
Unless you have unspecified requirements, you really don't need a regular expression for this:
(in the language of your choice, of course!)
如果您需要从较大的字符串中提取不等于“000000000”的所有八个字符串,您可以使用
标识每个序列的第一个字符并提取从其索引开始的八个字符。
If you need to extract all eight character strings not equal to "000000000" from a larger string, you could use
to identify the first character of each sequence and extract eight characters starting with its index.
当然,人们会简单地检查一下
,但为了记录在案,人们可以轻松地使用
重量级正则表达式(Perl 中);-)
打印
gofigure ;-)
问候
rbo
Of course, one would simply check
but for the record, one could easily employ
heavyweight regex (in Perl) for that ;-)
prints
go figure ;-)
Regards
rbo
([1-9]**|\D*){8} 不会这样做吗?或者我在这里遗漏了一些东西(这实际上只是 ndim 的反面,这似乎应该有效)。
我假设选择的字符包含的数字以上。
好吧,那是错误的,所以博洛教授我的成绩合格了吗? (我喜欢reg表达式所以我真的很好奇)。
那个工作?
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).
That work?