[英]Using select rather than gsub to avoid multiple regex evaluations in Ruby
这是一个输出,需要多个正则表达式求值,但可以完成我想做的事情(删除文本以外的所有内容)。
words = IO.read("file.txt").
gsub(/\s/, ""). # delete white spaces
gsub(".",""). # delete periods
gsub(",",""). # delete commas
gsub("?","") # delete Q marks
puts words
# output
# WheninthecourseofhumaneventsitbecomesnecessaryIwanttobelieveyoureallyIdobutwhoamItoblameWhenthefactsarecountedthenumberswillbereportedLotsoflaughsCharlieIthinkIheardthatonetentimesbefore
看看这篇文章-Ruby gsub:有没有更好的方法 -我认为我将尝试进行匹配以在没有多个正则表达式评估的情况下完成相同的结果。 但是我没有得到相同的输出。
words = IO.read("file.txt").
match(/(\w*)+/)
puts words
# output - this only gets the first word
# When
这只会得到第一句话:
words = IO.read("file.txt").
match(/(...*)+/)
puts words
# output - this only gets the first sentence
# When in the course of human events it becomes necessary.
关于在匹配而不是gsub上获得相同输出(包括去除空格和非单词字符)的任何建议?
您可以在一个gsub操作中执行所需的操作:
s = 'When in the course of human events it becomes necessary.'
s.gsub /[\s.,?]/, ''
# => "Wheninthecourseofhumaneventsitbecomesnecessary"
您不需要为此进行多个正则表达式评估。
str = "# output - this only gets the first sentence
# When in the course of human events it becomes necessary."
p str.gsub(/\W/, "")
#=>"outputthisonlygetsthefirstsentenceWheninthecourseofhumaneventsitbecomesnecessary"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.