这里有一个输出需要多个regex求值,但得到了我想做的事情(删除除文本以外的所有内容)。
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 : is there a better way-我想我会尝试做一个匹配来完成相同的结果,而不需要多个regex求值但我没有得到相同的输出。
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"
关于ruby - 使用select而不是gsub来避免Ruby中的多个正则表达式求值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10293012/