views:

500

answers:

6

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.

A: 

Ruby has strip function, does that helps. http://railsforphp.com/reference/strings/trim

Priyank Bolia
no that is only for the end or beginning of a string i believe.
TenJack
+1  A: 

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!(/ +?,/, ',')
Steve Weet
This is on the right track! I need to catch multiple spaces, but this does not seem to be working for the following sentance: "Brazil , officially the Federative Republic of Brazil , is the largest country in South America and the only Portuguese-speaking country on that continent"
TenJack
In what way is it not working, it works fine for me. This is the adjusted result "Brazil, officially the Federative Republic of Brazil, is the largest country in South America and the only Portuguese-speaking country on that continent"
Steve Weet
You're right, it is working. I had another method after it that was messing things up. Thanks for your help.
TenJack
A: 
sentance.gsub!(/\([^\)]*\)/, '')
sentance.gsub!(/ ,/, ',')
Question Mark
+2  A: 

According to question edit, instead of doing

sentance.gsub!(/ ,/, ',')

do this

sentance.gsub!(/\s+,/, ',')

this will remove multiple spaces before the commas

nas
Important note: `\s` will look for any whitespace (tabs, newlines, spaces) NOT just space.
David Oneill
true David, \s for any white space / line separator
nas
+1  A: 
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/

Beanish
A: 

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.

rampion