Hey, I am using a reg expression to remove parentheses and the content between them from a ruby string. The problem is that this sometimes leaves a space before commas. I am not sure how to go about removing this space. I’ve been playing around with the following but it has not been working:
@char_count = 0
sentance.each_char{|char|
if char == ","
if sentance[@char_count-1] == 32
sentance[@char_count-1] = ""
end
end
@char_count += 1
}
Any help is appreciated!
Edit: sentance.gsub!(/ ,/, ‘,’) is working well, but now I am realizing that there are some places where there are multiple spaces before a comma. I need to account for this scenerio as well.
You can use the gsub method of the string class to do this.
gsub will return a new string with the space , string replaced by a comma. gsub! will change the string in place.
If you sometimes have multiple spaces with a trailing comma then you may wish to use a slightly modified regex to catch the multiple spaces