如何在 Ruby 中使用 RegEx 更改字符串中字母的大小写 [英] How to change case of letters in string using RegEx in Ruby

查看:37
本文介绍了如何在 Ruby 中使用 RegEx 更改字符串中字母的大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串:hEY"

Say I have a string : "hEY "

我想把它转换成嘿"

string.gsub!(/([a-z])([A-Z]+ )/, '\1'.upcase)

这就是我的想法,但是当我在 gsub 方法中使用 upcase 方法时,它似乎什么也没做.这是为什么?

That is the idea I have, but it seems like the upcase method does nothing when I use it within the gsub method. Why is that?

我想出了这个方法:

string.gsub!(/([a-z])([A-Z]+ )/) { |str| str.downcase!.capitalize! }

有没有办法在正则表达式中做到这一点?我真的不明白 '\1' '\2' 的事情.那是反向引用吗?这是如何工作的

Is there a way to do this within the regex though? I don't really understand the '\1' '\2' thing. Is that backreferencing? How does that work

推荐答案

@sawa 答案很简单,而且您已经使用另一种机制编辑了您的问题.但是,要回答您的两个问题:

@sawa Has the simple answer, and you've edited your question with another mechanism. However, to answer two of your questions:

有没有办法在正则表达式中做到这一点?

不,Ruby 的正则表达式不支持大小写更改功能,如 其他一些正则表达式风格.您可以通过查看 1.9 和 2.0 的官方 Ruby 正则表达式文档并搜索单词case"来证明"这一点:

No, Ruby's regex does not support a case-changing feature as some other regex flavors do. You can "prove" this to yourself by reviewing the official Ruby regex docs for 1.9 and 2.0 and searching for the word "case":

我真的不明白 '\1' '\2' 的东西.那是反向引用吗?这是如何运作的?

您对 \1 的使用是一种反向引用.当您在搜索模式中使用 \1 等时,可以使用反向引用.例如,正则表达式 /f(.)\1/ 将找到字母 f,后跟任意字符,后跟同一个字符(例如foo"或f!!").

Your use of \1 is a kind of backreference. A backreference can be when you use \1 and such in the search pattern. For example, the regular expression /f(.)\1/ will find the letter f, followed by any character, followed by that same character (e.g. "foo" or "f!!").

在这种情况下,在传递给像 String#gsub 这样的方法的替换字符串中,反向引用确实引用了前一个捕获.来自文档:

In this case, within a replacement string passed to a method like String#gsub, the backreference does refer to the previous capture. From the docs:

"如果替换是一个字符串,它将被匹配的文本替换.它可能包含对格式 \d 的模式捕获组的反向引用,其中 d 是组号,或 \k,其中 n 是组名.如果是双引号字符串,则两个反向引用必须在前面加上一个反斜杠."

"If replacement is a String it will be substituted for the matched text. It may contain back-references to the pattern’s capture groups of the form \d, where d is a group number, or \k<n>, where n is a group name. If it is a double-quoted string, both back-references must be preceded by an additional backslash."

实际上,这意味着:

"hello world".gsub( /([aeiou])/, '_\1_' )  #=> "h_e_ll_o_ w_o_rld"
"hello world".gsub( /([aeiou])/, "_\1_" )  #=> "h_\u0001_ll_\u0001_ w_\u0001_rld"
"hello world".gsub( /([aeiou])/, "_\\1_" ) #=> "h_e_ll_o_ w_o_rld"

现在,您必须了解代码何时运行.在您的原始代码中...

Now, you have to understand when code runs. In your original code…

string.gsub!(/([a-z])([A-Z]+ )/, '\1'.upcase)

...你正在做的是在字符串 '\1' 上调用 upcase(没有效果)然后 then 调用 '\1'code>gsub! 方法,传入一个正则表达式和一个字符串作为参数.

…what you are doing is calling upcase on the string '\1' (which has no effect) and then calling the gsub! method, passing in a regex and a string as parameters.

最后,实现相同目标的另一种方法是使用像这样的块形式:

Finally, another way to achieve this same goal is with the block form like so:

# Take your pick of which you prefer:
string.gsub!(/([a-z])([A-Z]+ )/){ $1.upcase << $2.downcase }
string.gsub!(/([a-z])([A-Z]+ )/){ [$1.upcase,$2.downcase].join }
string.gsub!(/([a-z])([A-Z]+ )/){ "#{$1.upcase}#{$2.downcase}" }

在 gsub 的块形式中,捕获的模式设置为全局变量 $1$2 等,您可以使用它们来构造替换字符串.

In the block form of gsub the captured patterns are set to the global variables $1, $2, etc. and you can use those to construct the replacement string.

这篇关于如何在 Ruby 中使用 RegEx 更改字符串中字母的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆