使用ruby删除特定字符串(电子邮件地址)中的所有空格



用户可以输入文本,但我获取数据的方式通常包含不必要的回车和空格。

为了删除这些内容,使输入看起来更像一个真实的句子,我使用了以下内容:

string.delete!("n")
string = string.squeeze(" ").gsub(/([.?!]) */,'1  ')

但在以下情况下,我在电子邮件中得到了一个意外的空格:

string = "Hey     what is nnn up joeblow@dude.com      n okay"

我得到以下信息:

"Hey what is up joeblow@dude.  com okay"

如何为字符串的电子邮件部分启用异常,以便获得以下内容:

"Hey what is up joeblow@dude.com okay"

已编辑

您的方法执行以下操作:

 string.squeeze(" ") # replaces each squence of " " by one space
 gsub(/([.?!] */, '1 ') # check if there is a space after every char in the between the brackets [.?!]
                         # and whether it finds one or more or none at all
                         # it adds another space, this is why the email address
                         # is splitted

我想你真正想要的是,如果标点符号后面没有空格,就加一个空格。你可以这样做。

string.gsub(/([.?!])W/, '1 ') # if there is a non word char after 
                                # those punctuation chars, just add a space

然后您只需要用一个空格替换每个空格字符序列。所以最后的解决方案是:

string.gsub(/([.?!])(?=W)/, '1 ').gsub(/s+/, ' ')
# ([.?!]) => this will match the ., ?, or !. and capture it
# (?=W) => this will match any non word char but will not capture it.
# so /([.?!])(?=W)/ will find punctuation between parenthesis that
# are followed by a non word char (a space or new line, or even 
# puctuation for example).
# '1 ' => 1 is for the captured group (i.e. string that match the 
# group ([.?!]) which is a single char in this case.), so it will add 
# a space after the matched group.

如果你可以摆脱挤压语句,那么使用Nafaa的答案是最简单的方法,但我列出了另一种方法,以防它有用:

string = string.split(" ").join(" ")

然而,如果你想保留挤压语句,你可以修改Nafaa的方法,并在挤压语句之后使用它:

string.gsub(/s+/, ' ').gsub('. com', '.com')

或者直接更改字符串:

string.gsub('.  com', '.com')

最新更新