使用正则表达式检查最少8位的字符串

时间:2021-05-07 19:29:49

I have regex as follows:

我有正则表达式如下:

     /^(\d|-|\(|\)|\+|\s){12,}$/

This will allow digits, (, ), space. But I want to ensure string contains atleast 8 digits. Some allowed strings are as follows:

这将允许数字,(,),空格。但我想确保字符串包含至少8位数字。一些允许的字符串如下:

      (1323  ++24)233
      24243434 43
      ++++43435++4554345  434

It should not allow strings like:

它不应该允许字符串:

     ((((((1213)))
     ++++232+++

3 个解决方案

#1


7  

Use Look ahead within your regex at the start..

在开始时使用你的正则表达式中的前瞻..

/^(?=(.*\d){8,})[\d\(\)\s+-]{8,}$/
  ---------------
          |
          |->this would check for 8 or more digits

(?=(.*\d){8,}) is zero width look ahead that checks for 0 to many character (i.e .*) followed by a digit (i.e \d) 8 to many times (i.e.{8,0})

(?=(。* \ d){8,})是零宽度向前看,检查0到多个字符(即。*)后跟一个数字(即\ d)8到多次(即{8,0} })

(?=) is called zero width because it doesnt consume the characters..it just checks

(?=)被称为零宽度,因为它不消耗字符..只是检查


To restict it to 14 digits you can do

要将它重写为14位数,你可以做到

/^(?=([^\d]*\d){8,14}[^\d]*$)[\d\(\)\s+-]{8,}$/

try it here

试试吧

#2


0  

Here's a non regular expression solution

这是一个非正则表达式解决方案

numbers = ["(1323  ++24)233", "24243434 43" , "++++43435++4554345  434", "123 456_7"]

numbers.each do |number|
  count = 0
  number.each_char do |char| 
    count += 1 if char.to_i.to_s == char
    break if count > 7
  end
  puts "#{count > 7}"
end

#3


0  

No need to mention ^, $, or the "or more" part of {8,}, or {12,}, which is unclear where it comes from.

无需提及^,$或{8,}或{12,}的“或更多”部分,但不清楚它来自哪里。

The following makes the intention transparent.

以下内容使意图透明化。

r = /
  (?=(?:.*\d){8})    # First condition: Eight digits
  (?!.*[^-\d()+\s])  # Second condition: Characters other than `[-\d()+\s]` should not be included.
/x

resulting in:

导致:

"(1323  ++24)233" =~ r #=> 0
"24243434 43" =~ r #=> 0
"++++43435++4554345  434" =~ r #=> 0
"((((((1213)))" =~ r #=> nil
"++++232+++" =~ r #=> nil

#1


7  

Use Look ahead within your regex at the start..

在开始时使用你的正则表达式中的前瞻..

/^(?=(.*\d){8,})[\d\(\)\s+-]{8,}$/
  ---------------
          |
          |->this would check for 8 or more digits

(?=(.*\d){8,}) is zero width look ahead that checks for 0 to many character (i.e .*) followed by a digit (i.e \d) 8 to many times (i.e.{8,0})

(?=(。* \ d){8,})是零宽度向前看,检查0到多个字符(即。*)后跟一个数字(即\ d)8到多次(即{8,0} })

(?=) is called zero width because it doesnt consume the characters..it just checks

(?=)被称为零宽度,因为它不消耗字符..只是检查


To restict it to 14 digits you can do

要将它重写为14位数,你可以做到

/^(?=([^\d]*\d){8,14}[^\d]*$)[\d\(\)\s+-]{8,}$/

try it here

试试吧

#2


0  

Here's a non regular expression solution

这是一个非正则表达式解决方案

numbers = ["(1323  ++24)233", "24243434 43" , "++++43435++4554345  434", "123 456_7"]

numbers.each do |number|
  count = 0
  number.each_char do |char| 
    count += 1 if char.to_i.to_s == char
    break if count > 7
  end
  puts "#{count > 7}"
end

#3


0  

No need to mention ^, $, or the "or more" part of {8,}, or {12,}, which is unclear where it comes from.

无需提及^,$或{8,}或{12,}的“或更多”部分,但不清楚它来自哪里。

The following makes the intention transparent.

以下内容使意图透明化。

r = /
  (?=(?:.*\d){8})    # First condition: Eight digits
  (?!.*[^-\d()+\s])  # Second condition: Characters other than `[-\d()+\s]` should not be included.
/x

resulting in:

导致:

"(1323  ++24)233" =~ r #=> 0
"24243434 43" =~ r #=> 0
"++++43435++4554345  434" =~ r #=> 0
"((((((1213)))" =~ r #=> nil
"++++232+++" =~ r #=> nil