2015-05-30 99 views
0

我想使用系統()裏面的紅寶石。系統內部的字符串()內容禁忌。所以我嘗試:如何在系統()中替換寶石內紅寶石gem

filenamenew = filename.gsub(/ /, '_').gsub(/(/, '|').gsub(/)/, ']') 

可悲的是我得到:

/home/sascha/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems /core_ext/kernel_require.rb:54:in require': /home/sascha/ownCloud/RubymineProjects/youtube_dlhelper/lib/youtube_dlhelper/ripper.rb:57: end pattern with unmatched parenthesis: /(/ (SyntaxError) unmatched close parenthesis: /)/ from /home/sascha/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in需要 '從./youtube_dlhelper.rb:24:in`'

能解決這個問題?

+0

什麼是你得到的文件名,你想什麼它看起來像在結束了嗎?你也錯了,你的代碼似乎沒有排隊... – Anthony

+0

在這種情況下,文件名是:Rednex - Cotton Eye Joe(官方音樂視頻)[HD] - RednexMusic com.m4a –

+0

你想要什麼看起來像到底? – Anthony

回答

1

另一個可讀的變化可能是

str = "Rednex - Cotton Eye Joe (Official Music Video) [HD] - RednexMusic com.m4a" 

pattern = /[a-zA-Z0-9\-\s\_]/ #=> set the pattern accepted 

#=> check each character keep only if they are in the pattern and join them 
str.split(//).keep_if{|chr| chr =~ pattern}.join #=> "Rednex - Cotton Eye Joe Official Music Video HD - RednexMusic comm4a" 
2

,因爲你需要躲避括號中的正則表達式您收到此錯誤,即

"Hello()".gsub(/\(/, ' World')

將返回的「Hello World)」

1

下面是一些正則表達式來擺脫括號,括號和空格:

/(\(|\)|\s|\[|\])/ 

"Rednex - Cotton Eye Joe (Official Music Video) [HD] - RednexMusic com.m4a".gsub(/(\(|\)|\s|\[|\])/, "") 

輸出:

"Rednex-CottonEyeJoeOfficialMusicVideoHD-RednexMusiccom.m4a"