V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shw1395
V2EX  ›  正则表达式

{n},能否每执行一次后, n=n+2

  •  
  •   shw1395 · 2012-01-08 12:01:05 +08:00 · 2771 次点击
    这是一个创建于 4713 天前的主题,其中的信息可能已经有所发展或是发生改变。
    示例:
    03,00,00,00,01,00,00,00,14,00,00,00,10,9f,1c,ae,d6,45,bb,78,b3,ea,\
    2b,94,c0,69,7c,74,07,33,03,1c,0f,00,00,00,01,00,00,00,10,00,00,00,05,85,87,\
    0a,20,bd,9e,68,5a,85,14,a9,82,bb,2b,e2,18,00,00,00,01,00,00,00,10,00,00,00,\
    3f,c8,cb,0b,c0,52,41,e5,8d,65,e9,44,8b,2d,07,c2,19,00,00,00,01,00,00,00,10,\
    00,00,00,83,b6,53,18,66,4e,6f,a2,45,e0,d7,60,9f,b9,58,20,14,00,00,00,01,00,\
    00,00,14,00,00,00,26,5d,05,07,d8,2f,a2,60,84,bd,83,7d,f5,21,80,a7,05,6f,5a,\
    (略)

    需求:
    03,00,00,00,01,00,00,00,14,00,00,00,10,9f,1c,ae,d6,45,bb,78,b3,ea,2b,94,c0,\
    69,7c,74,07,33,03,1c,0f,00,00,00,01,00,00,00,10,00,00,00,05,85,87,0a,20,bd,\
    9e,68,5a,85,14,a9,82,bb,2b,e2,18,00,00,00,01,00,00,00,10,00,00,00,3f,c8,cb,\
    0b,c0,52,41,e5,8d,65,e9,44,8b,2d,07,c2,19,00,00,00,01,00,00,00,10,00,00,00,\
    83,b6,53,18,66,4e,6f,a2,45,e0,d7,60,9f,b9,58,20,14,00,00,00,01,00,00,00,14,\
    00,00,00,26,5d,05,07,d8,2f,a2,60,84,bd,83,7d,f5,21,80,a7,05,6f,5a,85,04,00,\
    00,00,01,00,00,00,10,00,00,00,09,c2,54,bd,e4,ea,50,f2,6d,14,97,f2,9c,51,af,\
    6d,20,00,00,00,01,00,00,00,13,04,00,00,30,82,04,0f,30,82,02,f7,a0,03,02,01,\
    02,02,0a,19,8b,11,d1,3f,9a,8f,fe,69,a0,30,0d,06,09,2a,86,48,86,f7,0d,01,01,\

    我测试用的是 Emeditor 8.06,是 Perl 引擎,据说 Perl 5.6 之后,引入了递归。我是这样替换的:
    find: \\\n((..,){n})
    replace with: \1\\\n

    {n} 初始值 3,每替换一次需要加 2,才能得到需求结果。是否存在更有效的写法?而不是每换一行改一次花括号内的值。任何平台任何正则引擎/语法都可以,回帖时注明就可以了。谢谢。
    2 条回复    1970-01-01 08:00:00 +08:00
    lululau
        1
    lululau  
       2012-01-08 12:44:24 +08:00
    echo xxxx | perl -e '$l .= $_ while <>; $l =~ s#(\\\n)(.{9})#$2$1#g;print $l;'
    icerunz
        2
    icerunz  
       2012-01-08 13:05:29 +08:00
    @lululau v2ex总会出现很多Perl达人用一堆字符弄出LZ想要的东东,赞,哈哈。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1018 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:37 · PVG 05:37 · LAX 13:37 · JFK 16:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.