简体   繁体   English

在Ruby中,如何分割两个或多个空格或制表符?

[英]In Ruby, how do I split on two or more spaces or a tab?

With Ruby, how do I split into two or more spaces or a tab? 使用Ruby,如何分割成两个或更多的空格或制表符? that is I have: 那是我有:

2.4.0 :005 >   str = "a\t\tb   c d"
 => "a\t\tb   c d"

and applying my rules above, I would like the result to be: 并应用上述规则,我希望结果是:

["a", "", "b", "c d"]

since the consecutive tabs are capturing an empty string. 因为连续的制表符捕获一个空字符串。 But when I try the below: 但是当我尝试以下内容时:

2.4.0 :007 > str.split(/(?:[[:space:]][[:space:]]+|\t)/)
 => ["a", "b", "c d"]

The tabs are getting merged into a single [[:space:]] . 这些选项卡将合并为一个[[:space:]]

How do I adjust my regular expression to split into two or more spaces or a tab character? 如何调整正则表达式以分成两个或更多个空格或制表符?

You could try this: 您可以尝试以下方法:

"a\t\tb   c d".split(/\t| {2,}/)
#=> ["a", "", "b", "c d"]

"ab \t\t\tf".split(/\t| {2,}/)
#=> ["ab ", "", "", "f"]

Where \\t is for a tab and {2,} for two or more spaces . 其中\\t表示制表符{2,}表示两个或多个空格 Notice that there is a space before {2,} . 请注意, {2,}之前有一个空格

To include non-breaking spaces you could add to the expression, like this: 要包含不间断空格 ,可以在表达式中添加 ,如下所示:

str.split(/\t|[ |\u00A0]{2,}/)

Examples: 例子:

str = "a\t\tb \u00A0 c d"         #=> "a\t\tb   c d"
str.split(/\t|[ |\u00A0]{2,}/)    #=> ["a", "", "b", "c d"]

str = "ab \t\t\tf"                #=> "ab \t\t\tf"
str.split(/\t|[ |\u00A0]{2,}/)    #=> ["ab ", "", "", "f"]

Where [ |\ ]{2,} will check for 2 or more occurrences of either a space or non-breaking space . 其中[ |\ ]{2,}将检查是否有两次或多次出现空格不间断空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM