代码之家  ›  专栏  ›  技术社区  ›  sbha

Julia正则表达式使下一个字符变为大写

  •  0
  • sbha  · 技术社区  · 6 年前

    如何将字符串中每个单词的第一个字母大写?使用 \1\U\2 作为…的一部分 replace() 引发错误: Bad replacement string . 正则表达式是首选的,但也欢迎使用其他方法。这是我期望的工作,但给出了一个错误:

    test_string = "the quick brown fox jumps over the lazy dog"
    replace(test_string, r"(^|\s)([a-z])" => s"\1\U\2")
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Bogumił Kamiński    6 年前

    你可以用 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 (默认设置)。

        2
  •  2
  •   sbha    6 年前

    下面是另一个使用 replace , uppercase

    replace(test_string, r"(^|\s)([a-z])" => uppercase)