TenJack
TenJack

Reputation: 1632

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 places where there are multiple spaces before a comma. I need to account for this scenerio as well.

Upvotes: 0

Views: 4009

Answers (8)

Gaurav Patil
Gaurav Patil

Reputation: 1382

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."

Upvotes: 1

chinacheng
chinacheng

Reputation: 211

I try many method, but the best way is that:

s.gsub(/[[:space:]]/,'')

this will remove all the space chars.

Upvotes: 1

rampion
rampion

Reputation: 89093

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.

Upvotes: 0

Beanish
Beanish

Reputation: 1662

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/

Upvotes: 1

nas
nas

Reputation: 3696

According to question edit, instead of doing

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

do this

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

this will remove multiple spaces before the commas

Upvotes: 2

Question Mark
Question Mark

Reputation: 3606

sentance.gsub!(/\([^\)]*\)/, '')
sentance.gsub!(/ ,/, ',')

Upvotes: 0

Steve Weet
Steve Weet

Reputation: 28402

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!(/ +?,/, ',')

Upvotes: 5

Priyank Bolia
Priyank Bolia

Reputation: 14451

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

Upvotes: 0

Related Questions