ruby regular_expressions.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby regular_expressions.rb相关的知识,希望对你有一定的参考价值。

require 'pry'

# Determine whether a string contains a SIN (Social Insurance Number).
# A SIN is 9 digits and we are assuming that they must have dashes in them
def has_sin?(string)
  return !((string.match(/(\b)(\d{3})-(\d{3})-(\d{3})(\b)/)).nil?)
end


puts "has_sin? returns true if it has what looks like a SIN"
puts (has_sin?("please don't share this: 234-604-142") == true)
print "\n"
puts "has_sin? returns false if it doesn't have a SIN"
puts has_sin?("please confirm your identity: XXX-XXX-142") == false
puts has_sin?("please don't share this: 234-6043-142") == false
puts has_sin?("please don't share this: 2342-604-142") == false
puts has_sin?("please don't share this: 234-604-1421") == false

# Return the Social Insurance Number from a string.
def grab_sin(string)
  @sin = string.scan(/\b\d{3}-\d{3}-\d{3}\b/)
  return @sin[0]
end

print "\n"
puts "grab_sin returns an SIN if the string has an SIN"
puts grab_sin("please don't share this: 234-604-142") == "234-604-142"

print "\n"
puts "grab_sin returns nil if it doesn't have a SIN"
puts grab_sin("please confirm your identity: XXX-XXX-142") == nil


# Return all of the SINs from a string, not just one.
def grab_all_sins(string)
  @all_sins = string.scan(/\b\d{3}-\d{3}-\d{3}\b/)
  return @all_sins
end

print "\n"
puts "grab_all_sins returns all SINs if the string has any SINs"
puts grab_all_sins("234-604-142, 350-802-074, 013-630-876") == ["234-604-142", "350-802-074", "013-630-876"]

puts "grab_all_sins returns an empty Array if it doesn't have any SINs"
puts grab_all_sins("please confirm your identity: XXX-XXX-142") == []


# Obfuscate all of the Social Insurance numbers in a string. Example: XXX-XX-4430.
def hide_all_sins(string)
  sins_array = string.scan(/\b\d{3}-\d{3}-\d{3}\b/)
  if (sins_array.nil? || sins_array.length == 0)
    sins_array = string
    return sins_array
  end
  sins_array.each do |each_sin|
    each_sin.gsub!(/\b\d{3}-\d{3}\b/, "XXX-XXX")
  end
  return sins_array.join(", ")
end

print "\n"
puts "hide_all_sins obfuscates any SINs in the string"
puts (hide_all_sins("234-601-142, 350-801-074, 013-601-876") == "XXX-XXX-142, XXX-XXX-074, XXX-XXX-876")

print "\n"
puts "hide_all_sins does not alter a string without SINs in it"
string = "Please confirm your identity: XXX-XXX-142"
puts (hide_all_sins(string) == string)


# Ensure all of the Social Insurance numbers use dashes for delimiters.
# Example: 480.014.430 and 480014430 would both be 480-014-430.
def format_sins(string)
  string.gsub!(/\b(\d{3})\W?(\d{3})\W?(\d{3})\b/, '\1-\2-\3')
  return string
end

print "\n"
puts "format_sins finds and reformat any SINs in the string"
puts (format_sins("234600142, 350.800.074, 013-600-876") == "234-600-142, 350-800-074, 013-600-876")

print "\n"
puts "format_sins does not alter a string without SINs in it"
string = "please confirm your identity: 4421422"
puts (format_sins(string) == string)

print "\n"
string = "please confirm your identity: 123abc445"
puts (format_sins(string) == string)

以上是关于ruby regular_expressions.rb的主要内容,如果未能解决你的问题,请参考以下文章

如何在第 n 次出现管道“|”后获取字符或字符串ORACLE 中使用 REGULAR_EXPRESSION 的符号?

linux sed 替换

扩展正则表达式

NotePad++ 正则表达式 转

Linux下的grep搜索命令详解

shell_processing