Changing case of a letter with regex

前端 未结 3 1238
有刺的猬
有刺的猬 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:

    0 讨论(0)
  • 2021-01-23 07:28

    simple one.. according to http://www.regexe.com/ it works fine:

    search pattern -  other\.t(est)
    replace pattern -  other\.getTest\(\)
    

    Good luck..

    0 讨论(0)
  • 2021-01-23 07:35

    This works. You need two passes with search and replace. Reference from this answer: Is it possible to transform to lowercase using Eclipse's regex search and replace?

    search 1:  other\.([a-z])([a-z]*)
    replace 1: other.ABCDEFGHIJKLMNOPQRSTUVWXYZ$1$2
    
    search 2:  other\.(A)BCDEFGHIJKLMNOPQRSTUVWXYZa|A(B)CDEFGHIJKLMNOPQRSTUVWXYZb|AB(C)DEFGHIJKLMNOPQRSTUVWXYZc|ABC(D)EFGHIJKLMNOPQRSTUVWXYZd|ABCD(E)FGHIJKLMNOPQRSTUVWXYZe|ABCDE(F)GHIJKLMNOPQRSTUVWXYZf|ABCDEF(G)HIJKLMNOPQRSTUVWXYZg|ABCDEFG(H)IJKLMNOPQRSTUVWXYZh|ABCDEFGH(I)JKLMNOPQRSTUVWXYZi|ABCDEFGHI(J)KLMNOPQRSTUVWXYZj|ABCDEFGHIJ(K)LMNOPQRSTUVWXYZk|ABCDEFGHIJK(L)MNOPQRSTUVWXYZl|ABCDEFGHIJKL(M)NOPQRSTUVWXYZm|ABCDEFGHIJKLM(N)OPQRSTUVWXYZn|ABCDEFGHIJKLMN(O)PQRSTUVWXYZo|ABCDEFGHIJKLMNO(P)QRSTUVWXYZp|ABCDEFGHIJKLMNOP(Q)RSTUVWXYZq|ABCDEFGHIJKLMNOPQ(R)STUVWXYZr|ABCDEFGHIJKLMNOPQR(S)TUVWXYZs|ABCDEFGHIJKLMNOPQRS(T)UVWXYZt|ABCDEFGHIJKLMNOPQRST(U)VWXYZu|ABCDEFGHIJKLMNOPQRSTU(V)WXYZv|ABCDEFGHIJKLMNOPQRSTUV(W)XYZw|ABCDEFGHIJKLMNOPQRSTUVW(X)YZx|ABCDEFGHIJKLMNOPQRSTUVWX(Y)Zy|ABCDEFGHIJKLMNOPQRSTUVWXY(Z)z([a-z]*)
    replace 2: other.get$1$2$3$4$5$6$7$8$9$10$11$12$13$14$15$16$17$18$19$20$21$22$23$24$25$26$27
    
    0 讨论(0)
提交回复
热议问题