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条)

报告相同问题?