Deleting '\' and other special characters from a String in Ruby - ruby

First post here so sorry if this is rookie material. I'm trying to hack away a complicated array into a string and I'm having trouble deleting '\' and '"' characters. Any help is appreciated.
Currently I have this:
"\"-6,\", \"\\\"-3-\\\", \\\"1\\\"\", \"\\\"3-\\\", \\\"5\\\"\", \"\\\"7-\\\", \\\"11\\\"\", \"\\\"14-\\\", \\\"15\\\"\", \"\\\"17-\\\", \\\"20\\\"\""
When I'd like to have this:
"-6, -3-, 1, 3-, 5, 7-, 11, 14-, 15, 17-, 20"
Thank you!

You could try using each and removing any non-desired character within every value in the main array, like:
array.each{|x| x.gsub!(/[^0-9-,]/, '') }.to_s.gsub!(',,', ',')
# => ["-6,-3-,1,3-,5,7-,11,14-,15,17-,20"]
This takes the main array and for each value it replaces any character that's not numeric, nor a hyphen nor a comma with '' using the /[^0-9-,]/ regex and the gsub! method, and the second gsub! is to replace the double comma that remains from the first modification.
I know isn't so elegant, but could help you.

Use String#tr instead of gsub, as tr is much faster than gsub:
arr.select { |x| x.tr!('\\\"',''); x unless x.empty? }.join(",")

I assume you have an array like:
Arr = [-6, -3-, 1, 3-, 5, 7-, 11, 14-, 15, 17-, 20]
So try join method:
Arr.join(',')
Hope it helps..

You could use:
myArray.map { |item| item.gsub(/\\|"/, "") }.join(",").gsub(",,", ",")

Related

Splitting the string with last underscore

I have a string like "a_b_c" or "a_b_c_d" or "a_b_c_d_e". I want to split the string at the last underscore.
**input**
'a_b_c'
**output**
a_b
c
**input**
'a_b_c_d'
**output**
a_b_c
d
I have done the following:
a='a_b_c'
a=a.split('_')
last=a.pop
a.delete(last)
p a.join("_")
p last
and achieved the result, but I don't think this should be done this way. I hope there is some regular expression to achieve this. Is there anyone who can help me with this?
You can use String#rpartition that searches for a given pattern form the right end of the string and splits when it finds it.
'a_b_c_d_e'.rpartition(/_/)
=> ["a_b_c_d", "_", "e"]
s = 'a_b_c_d_e'
parts = s.rpartition(/_/)
[parts.first, parts.last]
=> ["a_b_c_d", "e"]
EDIT: applying advices from the comments:
'a_b_c_d_e'.rpartition('_').values_at(0,2)
=> ["a_b_c_d", "e"]
Do you really need to split? How about just replacing the _ with a space? e.g. using rindex and []=
a[a.rindex('_')] = ' '
I didn't do a benchmark, but split creates a new array, which typically requires more resources, at least in other languages.
EDIT: as the question was edited, its now clear the OP is asking for a list instead of a string output
You can also get values as below,
> a = a.split('_')
> a[0..-2].join('_')
# => "a_b_c_d"
> a[-1]
# => "e"
'a_b_c_d_e'.split /_(?!.*_)/
#=> ["a_b_c_d", "e"]
The negative lookahead (?!.*_) requires that following the match of the underscore there is no other underscore in the string.
Split it with regex:
a.split(/_(?=[^_]+$)/)
Explanation:
matches the character _ with positive Lookahead (?=[^_]+$)
Match a single character not present in the list below [^_]+ and
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Assuming you know this string follows this format:
str = 'a_b_c_d_e'
# Remainder
str[0...-2] # -> 'a_b_c_d'
# Last symbol
str[-1] # -> 'e'

Removing a character from string in Rspec

I am new to Rspec and I am facing a little issue.
text = page.find(:xpath,"some xpath").text
the code line above gets the value from a html element i.e. 15% or any other value under 100, I want to remove % sign from the value after and getting it to use for comparison. Can anybody help on this?
You can use String#sub to replace it:
'15%'.sub('%', '') #=> '15'
You can also use gsub in case there is more than one occurrence:
'15%%%'.gsub('%', '') #=> '15'

Search for all occurrences of a letter in a string with offsets

I have a string as follows:
--d--d-d---d--
I want to find all occurrences of 'd' in this string with their offsets.
However, doing the following only gives me back the first result:
irb(main):001:0> m = /d/.match "d--d-d---d"
=> #<MatchData "d">
irb(main):002:0> m.size
=> 1
What am I doing wrong? I thought match will match all occurrences of the regex in the string.
To get the offset, you can use a loop like this:
s = '--d--d-d---d--'
offset = 0
while md = /d/.match(s,offset)
p md.offset(0)[1]
# MatchDate#offset Returns a two-element array
# containing the beginning and ending offsets
offset = md.offset(0)[1]
end
The answer I am looking for is in fact on this question: How do I get the match data for all occurrences of a Ruby regular expression in a string?
Like I said, I thought the MatchData result should contain all occurrences of the match. (I got this impression from the Ruby core doc here: http://www.ruby-doc.org/core-2.0/MatchData.html).
So while I still don't understand that part completely, at least the answer above helps me to get to all the occurrences.
As a variant:
str = '--d--d-d---d--'
str.each_char.with_index.select{|el| el[0] == "d"}.map(&:last)
Result:
[2, 5, 7, 11]
Just position of letter started from 0. If you need it to start from 1 use with_index(1), so result will be:
[3, 6, 8, 12]
Regexp#match only runs the pattern once. MatchData can contain multiple matches and thus multiple offsets. The first one is the entire match, the others are the contents of the capture groups within the regexp. There's nothing in MatchData resulting from multiple applications of the regexp.
String#index produces offsets directly and can be easily used to iterate through the string.
s = '--d--d-d---d--'
[].tap{ |offsets| i=-1; while i = s.index('d', i+1); offsets << i; end }
=> [2, 5, 7, 11]

How do I remove everything after first whitespace or certain number of characters?

I have a date string that I need to simplify in Ruby:
2008-10-09 20:30:40
I only want the day portion:
2008-10-09
I'm looking for a gsub line that will strip everything after a set number of characters or the first whitespace.
I prefer to use as simple a solution as I can. Using gsub is needlessly complex. Either of these will do it:
str = '2008-10-09 20:30:40'
str[/(\S+)/, 1] #=> "2008-10-09"
str[0, 10] #=> "2008-10-09"
Literal solution:
date.gsub(/(.{10}).*/, '\1')
date.gsub(/\s.*/, '')
date[0, 10]
Better solution: Treat it as a DateTime object - then you can format it as you wish:
date = DateTime.now
date.strftime("%m-%d-%Y") # America
date.strftime("%d-%m-%Y") # Europe
If the format is consistantly like that,
'2008-10-09 20:30:40'[/[-\d]+/] # => "2008-10-19"
'2008-10-09 20:30:40'.split[0]

How do I remove a substring after a certain character in a string using Ruby?

How do I remove a substring after a certain character in a string using Ruby?
new_str = str.slice(0..(str.index('blah')))
I find that "Part1?Part2".split('?')[0] is easier to read.
I'm surprised nobody suggested to use 'gsub'
irb> "truncate".gsub(/a.*/, 'a')
=> "trunca"
The bang version of gsub can be used to modify the string.
str = "Hello World"
stopchar = 'W'
str.sub /#{stopchar}.+/, stopchar
#=> "Hello W"
A special case is if you have multiple occurrences of the same character and you want to delete from the last occurrence to the end (not the first one).
Following what Jacob suggested, you just have to use rindex instead of index as rindex gets the index of the character in the string but starting from the end.
Something like this:
str = '/path/to/some_file'
puts str.slice(0, str.index('/')) # => ""
puts str.slice(0, str.rindex('/')) # => "/path/to"
We can also use partition and rpartitiondepending on whether we want to use the first or last instance of the specified character:
string = "abc-123-xyz"
last_char = "-"
string.partition(last_char)[0..1].join #=> "abc-"
string.rpartition(last_char)[0..1].join #=> "abc-123-"

Resources