2014-12-09 55 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]/,'')