将单词替换为大写单词

发布于 2024-12-29 02:46:29 字数 298 浏览 1 评论 0原文

如果该行的第一个单词(一个或多个)都是大写字母,我想用大写单词替换这些单词(使用 ruby​​ 的 .capitalize)。对于例如“FOO BAR”到“Foo Bar”,

我尝试了以下操作:

line.gsub!(/^([A-Z ]+)/, '\1'.capitalize)

两者

line.gsub!(/^([A-Z ]+)/, "\\1".capitalize)

都不起作用。有办法做到这一点吗?

If the first words of the line (one or more) are all in CAPs, I would like to replace those words with the capitalized words (using ruby's .capitalize). For e.g. "FOO BAR" to "Foo Bar"

I tried the following:

line.gsub!(/^([A-Z ]+)/, '\1'.capitalize)

and

line.gsub!(/^([A-Z ]+)/, "\\1".capitalize)

which both did not work. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆伤 2025-01-05 02:46:29

尝试:

line.gsub!(/^([A-Z ]+)/) { |w| w.capitalize }

在 IRB:

require 'active_support'
'FOO bar'.gsub(/^[A-Z]+/) { |w| w.capitalize }
 => "Foo bar" 

或 OP 的版本中:

'FOO bar'.gsub!(/^([A-Z ]+)/) { |w| w.capitalize }
 => "Foo bar" 

对于前两个单词,这又快又脏:

'FOO BAR'.gsub!(/^([A-Z ]+ [A-Z]+)/) { |w| w.capitalize }
 => "Foo bar" 

您可以使用以下方法变得更漂亮:

'FOO BAR'.gsub!(/^((?<word>[A-Z]+) \g<word>)/) { |w| w.capitalize }
 => "Foo bar" 

当然,使用 gsub< 的 ! 版本/code> 在固定字符串上不会做任何有用的事情。


OP 添加了额外的限制:

require 'active_support'

line = 'AFOO BFOO CFOO DFOO e f g'
words = line[/^(?:[A-Z]+ )+/].split.map{ |w| w.capitalize } # => ["Afoo", "Bfoo", "Cfoo", "Dfoo"] 
[words,line.split[words.size..-1]].join(' ')                # => "Afoo Bfoo Cfoo Dfoo e f g" 

Try:

line.gsub!(/^([A-Z ]+)/) { |w| w.capitalize }

In IRB:

require 'active_support'
'FOO bar'.gsub(/^[A-Z]+/) { |w| w.capitalize }
 => "Foo bar" 

or the OP's version:

'FOO bar'.gsub!(/^([A-Z ]+)/) { |w| w.capitalize }
 => "Foo bar" 

For the first two words, this is quick and dirty:

'FOO BAR'.gsub!(/^([A-Z ]+ [A-Z]+)/) { |w| w.capitalize }
 => "Foo bar" 

You can get a little prettier using:

'FOO BAR'.gsub!(/^((?<word>[A-Z]+) \g<word>)/) { |w| w.capitalize }
 => "Foo bar" 

Of course, using the ! version of gsub on a fixed string won't do anything useful.


The OP added additional constraints:

require 'active_support'

line = 'AFOO BFOO CFOO DFOO e f g'
words = line[/^(?:[A-Z]+ )+/].split.map{ |w| w.capitalize } # => ["Afoo", "Bfoo", "Cfoo", "Dfoo"] 
[words,line.split[words.size..-1]].join(' ')                # => "Afoo Bfoo Cfoo Dfoo e f g" 
云巢 2025-01-05 02:46:29

您想将一行中的所有单词大写,对吗?
尝试使用 String#scan 代替:

line.scan(/\w+|\W+/).map(&:capitalize).join

You want to capitalize all words in line, correct?
Try String#scan instead:

line.scan(/\w+|\W+/).map(&:capitalize).join
北座城市 2025-01-05 02:46:29

我不是 Ruby 程序员,但我可以看到您正在对字符串 \1 调用 capitalize,这当然只是 \1 > 再次。您将需要寻找类似于 PHP 的 preg_replace_callback 的东西,它允许您运行正则表达式通过函数进行匹配 - 在本例中为capitalize

I'm no Ruby programmer, but I can see that you're calling capitalize on the string \1, which of course is just \1 again. You will want to look for something similar to PHP's preg_replace_callback that will allow you to run regex matches through a function - in this case, capitalize.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文