Ruby 将 gsub 与正则表达式结合使用
Ruby using gsub with regex
好的,所以我尝试使用带有正则表达式的 gsub 来仅替换整个单词,而不是其中的一部分。规则之一是将"be"改为b。但我只想对个别单词做。
原始字符串: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
下面的代码将接收一个字符串数组,应该将 "be" 更改为 "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
您可以按如下方式进行。
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
我使用了 Hash#default_proc= to attach a proc to rules
so that rules[k]
returns k
if rules
does not have a key k
and used the form of String#gsub,它使用散列来进行替换。
/\bword\b/
查找单词 word
,而不是变量 word
定义的字符串。只需在您的代码中将此正则表达式更改为 /\b#{pattern}\b/
或 /\b#{pattern}\b/i
(不区分大小写),您的方法就会起作用。
这个替换器输出一个新的数组,没有改变原来的数组:
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
好的,所以我尝试使用带有正则表达式的 gsub 来仅替换整个单词,而不是其中的一部分。规则之一是将"be"改为b。但我只想对个别单词做。
原始字符串: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
下面的代码将接收一个字符串数组,应该将 "be" 更改为 "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
您可以按如下方式进行。
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
我使用了 Hash#default_proc= to attach a proc to rules
so that rules[k]
returns k
if rules
does not have a key k
and used the form of String#gsub,它使用散列来进行替换。
/\bword\b/
查找单词 word
,而不是变量 word
定义的字符串。只需在您的代码中将此正则表达式更改为 /\b#{pattern}\b/
或 /\b#{pattern}\b/i
(不区分大小写),您的方法就会起作用。
这个替换器输出一个新的数组,没有改变原来的数组:
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