简体   繁体   中英

gsub for removing brackets and \ from string

I have issues using gsub to remove [] and \\" from a string. Here is the string:

"[\"This is a word ect\", \"Char2\", \"Another grooup\", \"Char4\"]"

I want the returned value to be:

"This is a word ect, Char2, Another grooup, Char4"

Can anyone point me in the right direction?

Just out of curiosity:

str = '["This is a word ect", "Char2", "Another grooup", "Char4"]'
require 'json'
JSON.parse(str).join(', ')
#⇒ "This is a word ect, Char2, Another grooup, Char4"

You can use gsub :

=> %Q|"[\"This is a word ect\", \"Char2\", \"Another grooup\", \"Char4\"]"|
=> s.gsub(/"|\[|\]/, '')
=> "This is a word ect, Char2, Another grooup, Char4"

gsub for me " or [ or ] .

| - or operator.

\\ - escape [ and ]

You can use regex for that:

a = '"["Char1", "Char2", "Char3", "Char4"]"'
a.scan(/[a-zA-Z0-9]+/)
# => ["Char1", "Char2", "Char3", "Char4"]

After Question Edit:

a = '"[\"This is a word ect\", \"Char2\", \"Another grooup\", \"Char4\"]"'
a.split('\"').find_all { |a| a =~ /\w+/ }
# => ["This is a word ect", "Char2", "Another grooup", "Char4"]

Parsing the data with JSON is a safe and effective approach when the data is known to be well formed and parsable:

str = "[\"This is a word ect\", \"Char2\", \"This Element uses a (\\\") symbol that is important to keep\", \"Char4\"]"
require 'json'
JSON.parse(str).join(', ')
 => "This is a word ect, Char2, This Element uses a (\") symbol that is important to keep, Char4"

Otherwise a gsub solution using either regex or simple method chaining can be employed, but this sort of naive approach may remove quotation marks and brackets from the inner elements of the array string being parsed, potentially mangling the data you meant to extract.

str = "[\"This is a word ect\", \"Char2\", \"This Element uses a (\\\") symbol that is important to keep\", \"Char4\"]"
str.gsub('"', '').gsub('[','').gsub(']','')
 => "This is a word ect, Char2, This Element uses a (\\) symbol that is important to keep, Char4"

Notice how the gsub approach has a different result than the JSON parse method.

In theory, Ruby's eval could also be used to parse the string data into an array and then join it, but eval is meant for interpreting strings and running them as ruby code, and as such should only be used when it is important to run arbitrary ruby code that has been encoded as a string. The method name 'eval' actually comes from the word 'evaluate', not evil. Despite this, however, evaluation is not an objective in this scenario; parsing is.

Another reason why people are hesitant to recommend eval for trivial tasks like data parsing is that the worst case scenario of JSON#parse is that it fails to parse. Whereas the worst case scenario of eval is that you've completely deleted your file system by parsing a string that you didn't expect to be there when you first designed your code.

I don't think gsub is the right way to go.

eval("[\"This is a word ect\", \"Char2\", \"Another grooup\", \"Char4\"]")
.join(", ")
# => "This is a word ect, Char2, Another grooup, Char4"

Keep in mind that eval() can be dangerous. The reason it is not dangerous in this circumstance is because we are dealing with a fixed string. For more information, have a look at When is `eval` in Ruby justified?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM