2

この正規表現に問題があり、タイトルを取得していないようです(最後の「。」以降のすべて)。複数のソースで正規表現をテストしましたが、すべて正しいグループを取得しているようです。私が間違っているのはスキャン機能の問題ですか?

##### data.csv file #####
## Web_Sites.Shopping.Newegg,...
## Web_Sites.Shopping.Newegg_Secure,...
## Web_Sites.Shopping.O'Reilly_Books,...
## Web_Sites.Shopping.PackageTrackr,...
#####

## Grab the title from the list
regex = '([\w_-]+)$'

## Open the CSV File
data_file = CSV.open("data.csv", "r")

## Set the file we will append the data.
my_file = File.new("titles.csv", 'a')

## For each line in the data file, get the correct title
data_file.each do |data|
   note = data[0]
   title = note.scan(regex)
   my_file.print "#{note} : #{title}"
end

ありがとう、
LF4

4

1 に答える 1

6

あなたはscan正規表現の引数を与えていません、あなたはそれにプレーンな文字列を与えています、そしてその文字列'([\w_-]+)$'はおそらくあなたのどこにも現れないnoteのでscan何も役に立ちません。Regexpクラスを使用して、正規表現を作成および保存します。

regex = Regexp.new('([\w_-]+)$')

または(工藤あ​​りがとう正規表現リテラル形式の1つを使用できます。

regex = /([\w_-]+)$/
regex = %r{([\w_-]+)$}

そして、そのインスタンスをに渡しRexexpますnote.scan

note.scan(regex)
于 2011-06-25T05:10:38.393 に答える