I am new to the Language.
I am trying to extract all characters till the escape character (\r).
I tried
text = "abcde\r"
text.match(/(.*)(\r)/)
I tried the same with gsub and gsub!. None are working.
If you happen to be dealing with the specific case of lines from a file, and you want to remove the line ending, you can use chop or chomp. For example:
require 'pp'
lines = [
"hello",
"there\r",
"how\r\n",
"are you?"
]
lines.each do |line|
pp line
pp line.chomp
puts '---'
end
Results in:
"hello"
"hello"
---
"there\r"
"there"
---
"how\r\n"
"how"
---
"are you?"
"are you?"
---
chop as it indiscriminately removes the last character: 'foo'.chop # => "fo". Both rstrip and chomp are the most used ways to do this.You can use String#slice passing the regular expression and the match to get. The 1 represents the first (and in this case the only) match.
text = "abcde\r"
text.slice(/(.*)\r/, 1)
=> "abcde"
\risn't "the escape character", it's "an escaped character". Terminology is important.