2014-12-09 56 views
1

我已经创建了一个应用程序,例如使用rails的用户应用程序。在这个应用程序中,文本文件被导入到数据库。其中,我需要验证手机号码,这意味着它不应该包含+或*或任何其他特殊字符,假设它存在,它应该忽略此特殊字符并打印剩下的字符。我已经使用下面的代码来存储数组中的文本文件。具有正则表达式验证的Rails应用程序

File.open('text file') do |f| 
    while line = f.gets 
    array = line.split(',') 
    user = User.new 
    user.user_name = array[0] 
    user.email_id = array[1] 
    user.mobile_number = array[2] 
    user.save 
end 

回答

0

使用全局替代使用正则表达式来删除非数字部分。

user.mobile_number = array[2].gsub(/[^0-9]/,'')