2

这是一个需要多次正则表达式评估但得到我想做的输出的输出(删除除文本之外的所有内容)。

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 上获得相同输出(包括去除空格和非单词字符)的任何建议?

4

2 回答 2

1

你可以在一个 gsub 操作中做你想做的事:

s = 'When in the course of human events it becomes necessary.'
s.gsub /[\s.,?]/, ''
# => "Wheninthecourseofhumaneventsitbecomesnecessary"
于 2012-04-24T06:54:39.390 回答
0

您不需要为此进行多次正则表达式评估。

str = "# output - this only gets the first sentence
# When in the course of human events it becomes necessary."
p str.gsub(/\W/, "")
#=>"outputthisonlygetsthefirstsentenceWheninthecourseofhumaneventsitbecomesnecessary"
于 2012-04-24T07:03:25.483 回答