这是一个需要多次正则表达式评估但得到我想做的输出的输出(删除除文本之外的所有内容)。
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 上获得相同输出(包括去除空格和非单词字符)的任何建议?