例如:The fox is red.x________
(将下划线视为空格)。如何删除空格和x?
修改
如果有这样的几行怎么办?
The fox is red.x________
The fox is grey.x________
The fox is blue.x________
The fox is green.x________
答案 0 :(得分:1)
你也可以越过每一行,然后砍掉它:
text.lines.map(&:rstrip).map(&:chop).join($/)
# => The fox is red.
# => The fox is grey.
# => The fox is blue.
# => The fox is green.
答案 1 :(得分:0)
回答原来的问题:
"The fox is red.x ".sub(/. *\z/, "")
# => "The fox is red."
更改后的问题答案:
text.gsub(/. *$/, "")
答案 2 :(得分:0)
text.gsub(/.\s*$/, "")