Regex Julia正则表达式使下一个字符大写
如何将字符串中每个单词的第一个字母大写?将Regex Julia正则表达式使下一个字符大写,regex,string,julia,Regex,String,Julia,如何将字符串中每个单词的第一个字母大写?将\1\U\2用作replace()的一部分会引发错误:错误的替换字符串。正则表达式更可取,但也欢迎使用其他方法。这是我期望的工作,但给出了错误: test_string = "the quick brown fox jumps over the lazy dog" replace(test_string, r"(^|\s)([a-z])" => s"\1\U\2") 您可以使用titlecase函数进行如下操作: julia> test_s
\1\U\2
用作replace()
的一部分会引发错误:错误的替换字符串
。正则表达式更可取,但也欢迎使用其他方法。这是我期望的工作,但给出了错误:
test_string = "the quick brown fox jumps over the lazy dog"
replace(test_string, r"(^|\s)([a-z])" => s"\1\U\2")
您可以使用
titlecase
函数进行如下操作:
julia> test_string = "the quick brown fox jumps over the lazy dog"
"the quick brown fox jumps over the lazy dog"
julia> titlecase(test_string, strict=false)
"The Quick Brown Fox Jumps Over The Lazy Dog"
对于更复杂的情况,您可以定义
wordsep
函数或将strict
更改为true
(默认值)。您可以使用titlecase
函数进行如下操作:
julia> test_string = "the quick brown fox jumps over the lazy dog"
"the quick brown fox jumps over the lazy dog"
julia> titlecase(test_string, strict=false)
"The Quick Brown Fox Jumps Over The Lazy Dog"
对于更复杂的情况,您可以定义
wordsep
函数或将strict
更改为true
(默认值)。下面是另一个使用replace
、大写
和正则表达式的选项:
replace(test_string, r"(^|\s)([a-z])" => uppercase)
下面是另一个使用
替换
、大写
和正则表达式的选项:
replace(test_string, r"(^|\s)([a-z])" => uppercase)