0

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

示例输入:

Cleveland Indians 5, Boston Redsox 4

我正在尝试拆分,字母和数字之间的空间

示例输出:

Cleveland Indians
5
Boston Redsox
4

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

/,|\s[0-9]/
4

4 回答 4

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

\s(?=\d): 一个空格,后跟一个使用前瞻的数字。

于 2013-07-22T02:25:08.647 回答
1

如果你把它分成两部分——一个在逗号 + 空格处,然后一个用于将团队名称与分数分开——它可能会更清晰一些,特别是如果你必须在逗号之前添加更多选项,比如空格(现实世界的数据变得混乱!):

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

生成的列表列表也是一个更有意义的分组。

于 2013-07-22T02:37:21.847 回答
0
"Cleveland Indians 5, Boston Redsox 4".split(/\s*(\d+)(?:,\s+|\z)/)
# => ["Cleveland Indians", "5", "Boston Redsox", "4"]
于 2013-07-22T05:05:29.720 回答
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
于 2013-07-22T05:36:46.790 回答