Changing case of a letter with regex

前端 未结 3 1237
有刺的猬
有刺的猬 2021-01-23 06:54

I am using regular expressions in Eclipse IDE. I am trying to turn

other.test into other.getTest()

Search: other.([a-z])([a-z]*

3条回答
  •  生来不讨喜
    2021-01-23 07:27

    Unfortunately, Eclipse Find/Replace regex does not support case modifying operators like \U, \u, \L and \l. You may either use a long workaround suggested by jrahhali, or use Notepad++:

    Search: other\.([a-z]+) Replace: other.get\u$1\(\)

    Explantion:

    • other\. - matches a string other. (note the dot must be escaped to match a literal dot)
    • ([a-z]+) - Group 1 capturing 1 or more lowercase ASCII letters (check Match case option to only match lowercase ASCII letters with [a-z]+)

    Replacement pattern details:

    • other.get - a literal text other.get
    • \u$1 - the contents of Group 1 (the $1 is a backreference to the captured group 1) and its first character is turned to upper case with \u operator (\U would turn the whole text of the capture group to upper case)
    • \(\) - a literal text () (the parentheses should be escaped in NPP Boost conditional replacement patterns).

    Demo screen:

提交回复
热议问题