2018年4月19日 星期四

使用 Eclipse 的 Regular Expression Search 來轉大小寫

例如希望搜尋 x_ 開頭的字串,將 x_ 後面第一個字母轉小寫,也就是像 x_Abc 及 x_Efg 將被取代成 x_abc 及 x_efg

今天因工作需要取代大批檔案,而去找了一下方法,其實之前也有找過,但當時在網路上查到的都說不行,所以就把這事先放著,今天又去找了一下,嘿~,在 StackOverflow 上有人用了很有創意的方法來做到了,雖然不能一步驟完成,但也只需要兩步驟即可。讓我們來看一下他怎麼做
大寫轉小寫:

Step 1:
Search RegExp:([A-Z])
Replace With:abcdefghijklmnopqrstuvwxyz$1

註:上面只是寫重點部位,實際上當然不會只查 [A-Z],若以本文開頭的例子來說,則 Search RegExp 會是 x_([A-Z]),若還要再考慮前後的字元的話,就還要再加語法,否則不管寫在哪裡的大寫字母,都會被取代

Step 2:
Search RegExp:
(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
Replace With:
$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

這樣就能將大寫轉小寫了

小寫轉大寫:

Step 1:
Search RegExp:([a-z])
Replace With:ABCDEFGHIJKLMNOPQRSTUVWXYZ$1

Step 2:
Search RegExp:
(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
Replace With:
$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

參考來源:https://stackoverflow.com/questions/9181107/is-it-possible-to-transform-to-lowercase-using-eclipses-regex-search-and-replac/15280421

沒有留言:

張貼留言

廣告訊息會被我刪除