在换行符后使所有首字母大写

时间:2015-01-31 02:23:11

标签: ruby

我应该改变这个:

1.               this string has leading space and too    MANY tabs and sPaCes betweenX
2.   the indiVidual Words in each Line.X
3.  each Line ends with a accidentally  aDDED   X.X
4.            in this lab you wilL WRITE code that "sAnITizES" this string by normalizingX
5.   ("normalizing" means   capitalizing sentences   and setting otherX
6.  characters to lower case)     and removes        the extra spaces between WOrds.X

进入这个:

1.               This string has leading space and too    MANY tabs and sPaCes betweenX" 
2.   The indiVidual Words in each Line.X 
3.  Each Line ends with a accidentally  aDDED   X.X
4.            In this lab you wilL WRITE code that "sAnITizES" this string by normalizingX
5.   ("Normalizing" means   capitalizing sentences   and setting otherX
6.  Characters to lower case)     and removes         the extra spaces between WOrds.X

这就是我所拥有的:

string.gsub!(/[([a-zA-Z]+)][^\n]*/) { |word| word.capitalize!} 
print string

结果如下:

1.               This string has leading space and too    many tabs and sp
aces betweenx                                                             
2.   The individual words in each line.x                                   
3.  Each line ends with a accidentally  added   x.x                        
4.            In this lab you will write code that "sanitizes" this string 
by normalizingx                                                            
5.   ("normalizing" means   capitalizing sentences   and setting otherx    
6.  Characters to lower case)     and removes         the extra spaces betw
een words.x         

该号码是我定位的文字的一部分。一切都是一个大字符串:每个数字前面只有一个"\n""1""2""3""4"等等。有人能指出我正确的方向吗?

我可以将新行的第一个字母大写,将所有其他字母更改为小写。

2 个答案:

答案 0 :(得分:2)

如果您要更新文件内容,可以逐行读取文件,并使用

更新每一行
line.sub(/[a-z]/i) {|w| w.upcase}

如果要更新字符串中的内容,可以将字符串拆分为行并使用上一个方法更新,或者执行gsub这样的操作:

string.gsub(/^([^a-z]*)([a-z])/i) {|m| "#{$1}#{$2.upcase}"}

# regex: start of string, followed by zero or more non-alphabetic characters, followed by an alphabetic character

答案 1 :(得分:0)

我认为您在完全理解转换为代码所需的逻辑之前就已开始编码,这会导致您遇到的所有其他问题。如果我是你,我花一点时间用简单的英语向我自己描述转换规则是什么,并在开始编码之前验证它们是否包括所有情况,没有例外。这样做给了我规则:

  

对于每一行,将大写字母后跟一个"数字 -   dot - 0个或更多非单词字符"顺序,然后是一个   "任何角色 - 换行符"序列'

然后我将这些序列翻译成正则表达式,并且能够提出解决方案。

更多提示:

  • 如果要引用与正则表达式匹配的子字符串(用于组合转换后的字符串),请使用$1$2More info
  • rubular.com对于快速找出正则表达式开始无法匹配的位置非常有用。

这是我的解决方案。你可能想在看之前给你另一个刺。

str = <<EOF
1.               this string has leading space and too    MANY tabs and sPaCes betweenX
2.   the indiVidual Words in each Line.X
3.  each Line ends with a accidentally  aDDED   X.X
4.            in this lab you wilL WRITE code that "sAnITizES" this string by normalizingX
5.   ("normalizing" means   capitalizing sentences   and setting otherX
6.  characters to lower case)     and removes in       the extra spaces between WOrds.X
EOF

expected = <<EOF
1.               This string has leading space and too    MANY tabs and sPaCes betweenX
2.   The indiVidual Words in each Line.X
3.  Each Line ends with a accidentally  aDDED   X.X
4.            In this lab you wilL WRITE code that "sAnITizES" this string by normalizingX
5.   ("Normalizing" means   capitalizing sentences   and setting otherX
6.  Characters to lower case)     and removes in       the extra spaces between WOrds.X
EOF

# number - dot - >=1 nonword char - word char (this is what you need to upcase) - >=0 chars - newline
regex = /(\d\.\W+)(\w)(.*\n)/

str.gsub!(regex) { "#{$1}#{$2.upcase}#{$3}" }

puts expected
puts str == expected # true

从一位自学成才的程序员到另一位程序员的一些友好建议! :)