作者snowfly (@@)
看板Perl
標題[問題] 字串分割
時間Sun Jan 6 18:34:30 2013
請問各位前輩
如果我有一個字串(字數不一定)
我要每四個字元插入".",我該如何寫呢?
example
input: A1B2C3D4E5F6G7H8
output: A1B2.C3D4.E5F6.G7H8
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 36.239.6.76
1F:推 dryman:$str = "A1B2C3D4E5F6G7H8"; 01/06 20:25
2F:→ dryman:$str =~ s/(.{4})/\1\./g && $str =~ s/\.$//; 01/06 20:26
3F:→ dryman:print $str; ==> A1B2.C3D4.E5F6.G7H8 01/06 20:26
4F:推 dryman:喔,想到了更好的解法,把第二行換成 01/06 20:36
5F:→ dryman:$str =~ s/(.{4})(?=.)/\1\./g; 01/06 20:36
6F:→ snowfly:謝謝dryman , 我原本是要用join的方式, 01/12 23:22
7F:→ snowfly:但是想不出來每四個split的方法..@@ 01/12 23:23