So I've got a string that's an improperly formatted name. Let's say, "Jean-paul Bertaud-alain".
所以我有一个字符串是一个格式不正确的名字。让我们说,“Jean-paul Bertaud-alain”。
I want to use a regex in Ruby to find the first character after every dash and make it uppercase. So, in this case, I want to apply a method that would yield: "Jean-Paul Bertaud-Alain".
我想在Ruby中使用正则表达式来查找每个破折号后的第一个字符并将其设为大写。因此,在这种情况下,我想应用一种方法,可以产生:“Jean-Paul Bertaud-Alain”。
Any help?
有帮助吗?
3 个解决方案
#1
1
String#gsub
can take a block argument, so this is as simple as:
String#gsub可以采用块参数,所以这很简单:
str = "Jean-paul Bertaud-alain"
str.gsub(/-[a-z]/) {|s| s.upcase }
# => "Jean-Paul Bertaud-Alain"
Or, more succinctly:
或者,更简洁:
str.gsub(/-[a-z]/, &:upcase)
Note that the regular expression /-[a-z]/
will only match letters in the a-z
range, meaning it won't match e.g. à
. This is because String#upcase
does not attempt to capitalize characters with diacritics anyway, because capitalization is language-dependent (e.g. i
is capitalized differently in Turkish than in English). Read this answer for more information: https://*.com/a/4418681
请注意,正则表达式/ - [a-z] /将仅匹配a-z范围内的字母,这意味着它将不匹配,例如一个。这是因为String#upcase无论如何都不会尝试使用变音符号来大写字符,因为大写是依赖于语言的(例如,我在土耳其语中的表达方式与英语不同)。阅读此答案以获取更多信息:https://*.com/a/4418681
#2
0
"Jean-paul Bertaud-alain".gsub(/(?<=-)\w/, &:upcase)
# => "Jean-Paul Bertaud-Alain"
#3
0
I suggest you make the test more demanding by requiring the letter to be upcased: 1) be preceded by a capitalized word followed by a hypen and 2) be followed by lowercase letters followed by a word break.
我建议你通过要求提升信件来使测试更加苛刻:1)先写一个大写单词,然后是一个大写字母,然后是2)后跟小写字母后跟一个单词分隔符。
r = /
\b # Match a word break
[A-Z] # Match an upper-case letter
[a-z]+ # Match >= 1 lower-case letters
\- # Match hypen
\K # Forget everything matched so far
[a-z] # Match a lower-case letter
(?= # Begin a positive lookahead
[a-z]+ # Match >= 1 lower-case letters
\b # Match a word break
) # End positive lookahead
/x # Free-spacing regex definition mode
"Jean-paul Bertaud-alain".gsub(r) { |s| s.upcase }
#=> "Jean-Paul Bertaud-Alain"
"Jean de-paul Bertaud-alainM".gsub(r) { |s| s.upcase }
#=> "Jean de-paul Bertaud-alainM"
#1
1
String#gsub
can take a block argument, so this is as simple as:
String#gsub可以采用块参数,所以这很简单:
str = "Jean-paul Bertaud-alain"
str.gsub(/-[a-z]/) {|s| s.upcase }
# => "Jean-Paul Bertaud-Alain"
Or, more succinctly:
或者,更简洁:
str.gsub(/-[a-z]/, &:upcase)
Note that the regular expression /-[a-z]/
will only match letters in the a-z
range, meaning it won't match e.g. à
. This is because String#upcase
does not attempt to capitalize characters with diacritics anyway, because capitalization is language-dependent (e.g. i
is capitalized differently in Turkish than in English). Read this answer for more information: https://*.com/a/4418681
请注意,正则表达式/ - [a-z] /将仅匹配a-z范围内的字母,这意味着它将不匹配,例如一个。这是因为String#upcase无论如何都不会尝试使用变音符号来大写字符,因为大写是依赖于语言的(例如,我在土耳其语中的表达方式与英语不同)。阅读此答案以获取更多信息:https://*.com/a/4418681
#2
0
"Jean-paul Bertaud-alain".gsub(/(?<=-)\w/, &:upcase)
# => "Jean-Paul Bertaud-Alain"
#3
0
I suggest you make the test more demanding by requiring the letter to be upcased: 1) be preceded by a capitalized word followed by a hypen and 2) be followed by lowercase letters followed by a word break.
我建议你通过要求提升信件来使测试更加苛刻:1)先写一个大写单词,然后是一个大写字母,然后是2)后跟小写字母后跟一个单词分隔符。
r = /
\b # Match a word break
[A-Z] # Match an upper-case letter
[a-z]+ # Match >= 1 lower-case letters
\- # Match hypen
\K # Forget everything matched so far
[a-z] # Match a lower-case letter
(?= # Begin a positive lookahead
[a-z]+ # Match >= 1 lower-case letters
\b # Match a word break
) # End positive lookahead
/x # Free-spacing regex definition mode
"Jean-paul Bertaud-alain".gsub(r) { |s| s.upcase }
#=> "Jean-Paul Bertaud-Alain"
"Jean de-paul Bertaud-alainM".gsub(r) { |s| s.upcase }
#=> "Jean de-paul Bertaud-alainM"