分裂在字母和数字之间的空间

时间:2013-07-22 02:21:34

标签: ruby regex

我正在用这个简单的正则表达式度过最糟糕的时光。

示例输入:

Cleveland Indians 5, Boston Redsox 4

我正试图在,和字母和数字之间的空格处分开

示例输出:

Cleveland Indians
5
Boston Redsox
4

这是我到目前为止所拥有的,但它仍包括数字。

/,|\s[0-9]/

4 个答案:

答案 0 :(得分:4)

string = "Cleveland Indians 5, Boston Redsox 4"
string.split /,\s*|\s(?=\d)/
# => ["Cleveland Indians", "5", "Boston Redsox", "4"] 

\s(?=\d):使用lookahead后跟数字的空格。

答案 1 :(得分:1)

如果你把它分成两个分区 - 一个在逗号+空格,然后一个用于将团队名称与分数分开 - 它可能会更清晰一点,特别是如果你必须添加更多选项,如空格< em>之前逗号(实际数据变得混乱!):

scores = "Cleveland Indians 5, Boston Redsox 4"
scores.split(/,\s*/).map{|score| score.split(/\s+(?=\d)/)}
 => [["Cleveland Indians", "5"], ["Boston Redsox", "4"]]

结果列表列表也是一个更有意义的分组。

答案 2 :(得分:0)

"Cleveland Indians 5, Boston Redsox 4".split(/\s*(\d+)(?:,\s+|\z)/)
# => ["Cleveland Indians", "5", "Boston Redsox", "4"]

答案 3 :(得分:0)

1)

str = "Cleveland Indians 15, Boston Red Sox 4"
phrases = str.split(", ")

phrases.each do |phrase|
  *team_names, score = phrase.split(" ")
  puts team_names.join " "
  puts score
end


--output:--
Cleveland Indians
15
Boston Red Sox
4

2)

str = "Cleveland Indians 15, Boston Red Sox 4"

pieces = str.split(/
    \s*       #A space 0 or more times
    (\d+)     #A digit 1 or more times, include match with results
    [,\s]*    #A comma or space, 0 or more times
/x)           

puts pieces



--output:--
Cleveland Indians
15
Boston Red Sox
4

第一次拆分为“15”,第二次拆分为“4” - 结果中包含得分。

3)

str = "Cleveland Indians 15, Boston Red Sox 4"

str.scan(/
    (
      \w      #Begin with a word character
      \D+     #followed by not a digit, 1 or more times
    )
    [ ]       #followed by a space
    (\d+)     #followed by a digit, one or more times
/x) {|capture_groups| puts capture_groups}


--output:--
Cleveland Indians
15
Boston Red Sox
4