如何使用正则表达式将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?

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?

Without regex: using str_replace()

<?php
$string = 'hello \o{test} how are you?';
echo str_replace(['\o{','}'],['<span>','</span>'],$string);
?>

DEMO: https://3v4l.org/ttSC2

With regex using preg_replace()

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

DEMO: https://3v4l.org/FiOKO