且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式替换和放大器;#58;到":"等等

更新时间:2022-01-30 22:15:28

使用正则表达式的一大优点是处理棘手的情况下,像&放大器;#38;#38; 实体更换不重复,这是一个单一的步骤。正则表达式也将是相当有效:两个主角是固定的,所以它会快速跳过任何无法与&放大器开始;#。最后,正则表达式的解决方案是一个没有很多的惊喜为将来的维护者。

The big advantage of using a regex is to deal with the tricky cases like & Entity replacement isn't iterative, it's a single step. The regex is also going to be fairly efficient: the two lead characters are fixed, so it will quickly skip anything not starting with &#. Finally, the regex solution is one without a lot of surprises for future maintainers.

我会说一个正则表达式是正确的选择。

I'd say a regex was the right choice.

这是***的正则表达式,关​​系吗?你知道你需要两位数字,如果你有3个数字,第一个将是1.可打印的ASCII毕竟是&放大器;#32; - &安培;#126; 。出于这个原因,你可以考虑&放大器;#1 \\ D \\ D;

Is it the best regex, though? You know you need two digits and if you have 3 digits, the first one will be a 1. Printable ASCII is after all  -~. For that reason, you could consider &#1?\d\d;.

至于更换的内容,我会使用basic算法描述的boost ::正则表达式替换::

As for replacing the content, I'd use the basic algorithm described for boost::regex::replace :

For each match // Using regex_iterator<>
    Print the prefix of the match
    Remove the first 2 and last character of the match (&#;)
    lexical_cast the result to int, then truncate to char and append.

Print the suffix of the last match.