Remove space before a comma in a ruby string
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 p开发者_运维知识库laces 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.
s = "this is a string, with some commas with spaces in front ,1 ,2 ,3"
s.gsub(/ ,/, ',')
"this is a string, with some commas with spaces in front,1,2,3"
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
s.gsub!(/ +?,/, ',')
According to question edit, instead of doing
sentance.gsub!(/ ,/, ',')
do this
sentance.gsub!(/\s+,/, ',')
this will remove multiple spaces before the commas
s.gsub(/\s*,/, ',')
The above regex will look for any number of whitespace(including 0) followed immediately by a comma.
Use a testing tool like the following to test your regex: http://www.gskinner.com/RegExr/
I try many method, but the best way is that:
s.gsub(/[[:space:]]/,'')
this will remove all the space chars.
Use Below regex to replace space between word and punctuation mark
str = "Okay . They , say ? about. how ; you ! made the following. special? creature react for further questioning details . I loved Pizza . Definitely . So seven . Yes ."
str.gsub(/\s+(?=[.,;?!])/, "")
Output str = "Okay. They, say? about. how; you! made the following. special? creature react for further questioning details. I loved Peter. Definitely. So seven. Yes."
Ruby has strip function, does that helps. http://railsforphp.com/reference/strings/trim
sentance.gsub!(/\([^\)]*\)/, '')
sentance.gsub!(/ ,/, ',')
You could also handle both the removal of parenthesized expressions and the leading space issue in a single regex:
sentence.gsub!(/\s*\([^\)]*\)/, '')
This will also handle cases where you have a parenthesized expression followed by some other punctuation (a period, semicolon, whatever), since the real issue is the space between whatever precedes the parenthesized expression and the opening paren. This will also leave any intentional uses of ' ,' in the text that weren't caused by removing a parenthesized expression.
精彩评论