2016-12-09 9 views
3

だから、正規表現でgsubを使って、単語全体だけを置き換えてみました。ルールの1つは、 "be"をbに変更することです。しかし、私は個々の言葉のためにそれをしたいだけです。正規表現でgsubを使っているRuby

オリジナルの文字列:I really want to be the best at everything

変更された文字列:I really want to be the best at everything

目的の文字列:I really want to b the best at everything

次のコードでは、文字列の配列になりますと、 "B" に "可能" に変更する必要があります。

array = [ 
"Hey guys, can anyone teach me how to be cool? 
I really want to be the best at everything, 
you know what I mean? Tweeting is super fun you guys!!!!", 
"OMG you guys, you won't believe how sweet my kitten is. 
My kitten is like super cuddly and too cute to be believed right?", 
"I'm running out of example tweets for you guys, which is weird, 
because I'm a writer and this is just writing and I tweet all day. 
For real, you guys. For real.", 
"GUISEEEEE this is so fun! I'm tweeting for you guys and this tweet is 
SOOOO long it's gonna be way more than you would think twitter can handle, 
so shorten it up you know what I mean? I just can never tell how long to keep typing!", 
"New game. Middle aged tweet followed by #youngPeopleHashTag Example: 
Gotta get my colonoscopy and mammogram soon. Prevention is key! #swag" 
] 

rules = { 
    "hello" => "hi", 
    "too" => "2", 
    "to" => "2", 
    "two" => "2", 
    "for" => "4", 
    "four" => "4", 
    "be" => "b", 
    "you" => "u", 
    "at" => "@", 
    "and" => "&" 
} 

def substitutor(strings,subs) 
    strings.each_with_index do |string,index| 
     subs.each do |word,substitute| 
      strings[index].gsub!(/\bword\b/, substitute) 
     end 
    end 
end 

substitutor(array,rules) 

私は正規表現を使用せずに置換されている場合、それは私にこれを与えるだろうが:I really want to b the bst at everything

+0

メソッドの仮パラメータが間違っています。これは 'string'ですが、メソッド定義に従って' string'(複数形)にする必要があります。 –

+0

あなたの現在の正規表現は、単語の両側にある単語の境界線を探しますが、正しく見えます。出力を生成するために実際に使用したコードを正確に表示してください。 –

+0

'Regexp.union'はあなたの友人です。別のアプローチ:スペース上の単語に分割し、個別に再マップし、 'join'を単一の文字列に戻します。 – tadman

答えて

2

/\bword\b/は、変数wordで定義された文字列ではなく、単語wordを検索します。この正規表現を/\b#{pattern}\b/に変更するか、コード内で/\b#{pattern}\b/i(大文字と小文字は区別されません)に変更してください。

このsubstituorはオリジナルのものを変更せずに、新しい配列を出力します

def substitutor(strings,rules) 
    rules.inject(strings) do |strings, (pattern, replace)| 
    strings.map do |string| 
     string.gsub(/\b#{pattern}\b/i, replace) 
    end 
    end 
end 

puts substitutor(array,rules) 

# Hey guys, can anyone teach me how 2 b cool? 
# I really want 2 b the best @ everything, 
# u know what I mean? Tweeting is super fun u guys!!!! 
# OMG u guys, u won't believe how sweet my kitten is. 
# My kitten is like super cuddly & 2 cute 2 b believed right? 
# I'm running out of example tweets 4 u guys, which is weird, 
# because I'm a writer & this is just writing & I tweet all day. 
# 4 real, u guys. 4 real. 
# GUISEEEEE this is so fun! I'm tweeting 4 u guys & this tweet is 
# SOOOO long it's gonna b way more than u would think twitter can handle, 
# so shorten it up u know what I mean? I just can never tell how long 2 keep typing! 
# New game. Middle aged tweet followed by #youngPeopleHashTag Example: 
# Gotta get my colonoscopy & mammogram soon. Prevention is key! #swag 
0

次のようにあなたがそれを行うことができます。

rules.default_proc = ->(_,k) { k } 
arr = array.map { |s| s.gsub(/\w+/, rules) } 

これは、以下を生成する。

arr.each { |s| puts s } 
    # Hey guys, can anyone teach me how 2 b cool? 
    # I really want 2 b the best @ everything, 
    # u know what I mean? Tweeting is super fun u guys!!!! 
    # OMG u guys, u won't believe how sweet my kitten is. 
    # My kitten is like super cuddly & 2 cute 2 b believed right? 
    # I'm running out of example tweets 4 u guys, which is weird, 
    # because I'm a writer & this is just writing & I tweet all day. 
    # For real, u guys. For real. 
    # GUISEEEEE this is so fun! I'm tweeting 4 u guys & this tweet is 
    # SOOOO long it's gonna b way more than u would think twitter can handle, 
    # so shorten it up u know what I mean? I just can never tell how long 2 keep typing! 
    # New game. Middle aged tweet followed by #youngPeopleHashTag Example: 
    # Gotta get my colonoscopy & mammogram soon. Prevention is key! #swag 

は私がキー kを持っていない rules rules場合 k戻り rules[k]ようにPROCを添付する Hash#default_proc=を使用して置換を行うためのハッシュを採用 String#gsubの形式を使用していました。

関連する問題