dorbmd1177 2018-09-02 16:16 采纳率: 100%
浏览 100
已采纳

如何使用正则表达式将tex标签转换为php或sed中的html标签? [关闭]

I have this tex:

hello \o{test} how are you?

I want to convert it into:

hello <span>test</span> how are you?

how?

  • 写回答

2条回答 默认 最新

  • dqqs64238 2018-09-02 16:31
    关注

    You may use the following expression:

    \\o\{([^}]+)}
    
    • \\o Matches o.
    • \{ Matches {.
    • ([^}]+) Capturing group. Matches and captures anything other than a }.
    • } Matches a }.

    Replacing with:

    <span>\1<\/span>
    

    Regex demo here.


    Sed implementation:

    $ echo "hello \o{test} how are you?" | sed -r 's/\\o\{([^}]+)}/<span>\1<\/span>/g'
    hello <span>test</span> how are you?
    

    Php implementation:

    <?php
    $input_lines="hello \o{test} how are you?";
    echo preg_replace("/\\\\o{([^}]+)}/", "<span>$1<\/span>", $input_lines);
    

    Prints:

    hello <span>test<\/span> how are you?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料