Ruby: Escaping special characters in a string
Your pattern isn't defined correctly in your example. This is as close as I can get to your desired output.
Output
"\\\"My\\\" \\'name\\' \\*is\\* \\-john\\- \\.doe\\. \\/ok?\\/ C:\\\\Drive"
It's going to take some tweaking on your part to get it 100% but at least you can see your pattern in action now.
def self.escape_characters_in_string(string)
pattern = /(\'|\"|\.|\*|\/|\-|\\)/
string.gsub(pattern){|match|"\\" + match} # <-- Trying to take the currently found match and add a \ before it I have no idea how to do that).
end
How to print an escape character in Ruby?
Use String#inspect
puts word.inspect #=> "x\nz"
Or just p
p word #=> "x\nz"
String literal without need to escape backslash
There is somewhat similar thing available in Ruby. E.g.
foo = %Q(The integer division operator in VisualBASIC is written "a \\ b" and #{'interpolation' + ' works'})
You can also interpolate strings in it. The only caveat is, you would still need to escape \
character.
HTH
Ruby : unable to find strings with special character in a file
You need to escape the back-slash. For example:
File.readlines("E:/nano/ABC.txt").grep(/ncr\\abc_efg_dev/).any?
Or for a fully generic solution, you can use Regexp#escape:
search_string = 'ncr\abc_efg_dev'
File
.readlines("E:/nano/ABC.txt")
.grep(Regexp.new(Regexp.escape(search_string)))
.any?
This is because, for example, \a
is a special character called the Bell code.
Other examples include \n
(newline), \t
(tab), \f
(form feed), \v
(vertical tab), \r
(a carriage return), \s
(any white-space character), \b
(a backspace or word boundary, depending on context), ...
Long story short, you should always escape \
in a regular expression (or any double-quoted string in ruby!), unless you are intentionally using it to denote a special character.
Ruby Match - escape a string with special characters
You probably want Regexp.escape
:
service = properties.match(/^com\.google\.(#{Regexp.escape(serviceName)})\.public$/)
Additionally, you had surrounded your inclusion of serviceName
with a [...]+
, which means more than one character from this list of characters in [...]
.
E.g. This regexp [commonapi]+
accepts moconaipimdconn
, or indeed any length string that contained some characters from the service name you actually wanted to capture.
Best way to escape and unescape strings in Ruby?
Ruby 2.5 added String#undump
as a complement to String#dump
:
$ irb
irb(main):001:0> dumped_newline = "\n".dump
=> "\"\\n\""
irb(main):002:0> undumped_newline = dumped_newline.undump
=> "\n"
With it:
def escape(s)
s.dump[1..-2]
end
def unescape(s)
"\"#{s}\"".undump
end
$irb
irb(main):001:0> escape("\n \" \\")
=> "\\n \\\" \\\\"
irb(main):002:0> unescape("\\n \\\" \\\\")
=> "\n \" \\"
Related Topics
How to Run Rake with --Trace Within Capistrano
Ruby Incorrectly Parses 2 Digit Year
Ruby - Extracting the Unique Values Per Key from an Array of Hashes
How to Extend Redcarpet to Support Auto Linking User Mentions
How to Convert a Scientific Notation String to Decimal Notation
How to Set Ca-Bundle Path for Openssl in Ruby
How to Add New View to Ruby on Rails Spree Commerce App
Ruby on Rails Country/State Select Enigma
Authlogic Perishable_Token Resets on Every Request
Is It a Good Idea to Purge Old Rails Migration Files
Rails 3.2.8 - How to Get the Week Number from Rails
Ruby Daemons and Jruby - Alternative Options
Rails: Violates Foreign Key Constraint
Why Is Uri.Escape() Marked as Obsolete and Where Is This Regexp::Unsafe Constant