<tfoot id='e5H4i'></tfoot>

  1. <i id='e5H4i'><tr id='e5H4i'><dt id='e5H4i'><q id='e5H4i'><span id='e5H4i'><b id='e5H4i'><form id='e5H4i'><ins id='e5H4i'></ins><ul id='e5H4i'></ul><sub id='e5H4i'></sub></form><legend id='e5H4i'></legend><bdo id='e5H4i'><pre id='e5H4i'><center id='e5H4i'></center></pre></bdo></b><th id='e5H4i'></th></span></q></dt></tr></i><div id='e5H4i'><tfoot id='e5H4i'></tfoot><dl id='e5H4i'><fieldset id='e5H4i'></fieldset></dl></div>

      • <bdo id='e5H4i'></bdo><ul id='e5H4i'></ul>
    1. <legend id='e5H4i'><style id='e5H4i'><dir id='e5H4i'><q id='e5H4i'></q></dir></style></legend>

      <small id='e5H4i'></small><noframes id='e5H4i'>

      如何转义字符串以在 Boost Regex 中使用

      时间:2023-06-29
      <tfoot id='nneu1'></tfoot>
      <legend id='nneu1'><style id='nneu1'><dir id='nneu1'><q id='nneu1'></q></dir></style></legend>
        <tbody id='nneu1'></tbody>

        • <i id='nneu1'><tr id='nneu1'><dt id='nneu1'><q id='nneu1'><span id='nneu1'><b id='nneu1'><form id='nneu1'><ins id='nneu1'></ins><ul id='nneu1'></ul><sub id='nneu1'></sub></form><legend id='nneu1'></legend><bdo id='nneu1'><pre id='nneu1'><center id='nneu1'></center></pre></bdo></b><th id='nneu1'></th></span></q></dt></tr></i><div id='nneu1'><tfoot id='nneu1'></tfoot><dl id='nneu1'><fieldset id='nneu1'></fieldset></dl></div>
          • <bdo id='nneu1'></bdo><ul id='nneu1'></ul>

              <small id='nneu1'></small><noframes id='nneu1'>

                本文介绍了如何转义字符串以在 Boost Regex 中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我刚开始了解正则表达式,我正在使用 Boost Regex 库.

                I'm just getting my head around regular expressions, and I'm using the Boost Regex library.

                我需要使用一个包含特定 URL 的正则表达式,它会阻塞,因为显然 URL 中有一些字符是为正则表达式保留的,需要转义.

                I have a need to use a regex that includes a specific URL, and it chokes because obviously there are characters in the URL that are reserved for regex and need to be escaped.

                Boost 库中是否有任何函数或方法可以为这种用法转义字符串?我知道在大多数其他正则表达式实现中都有这样的方法,但我在 Boost 中没有看到.

                Is there any function or method in the Boost library to escape a string for this kind of usage? I know there are such methods in most other regex implementations, but I don't see one in Boost.

                或者,是否有需要转义的所有字符的列表?

                Alternatively, is there a list of all characters that would need to be escaped?

                推荐答案

                . ^ $ | ( ) [ ] { } * + ? 
                

                具有讽刺意味的是,您可以使用正则表达式来对 URL 进行转义,以便将其插入到正则表达式中.

                Ironically, you could use a regex to escape your URL so that it can be inserted into a regex.

                const boost::regex esc("[.^$|()\[\]{}*+?\\]");
                const std::string rep("\\&");
                std::string result = regex_replace(url_to_escape, esc, rep,
                                                   boost::match_default | boost::format_sed);
                

                (标志boost::format_sed 指定使用 sed 的替换字符串格式.在 sed 中,转义 & 将输出与整个表达式匹配的任何内容)

                (The flag boost::format_sed specifies to use the replacement string format of sed. In sed, an escape & will output whatever matched by the whole expression)

                或者如果你对sed的替换字符串格式不满意,只需将标志更改为boost::format_perl,你可以使用熟悉的$&来引用到与整个表达式匹配的任何内容.

                Or if you are not comfortable with sed's replacement string format, just change the flag to boost::format_perl, and you can use the familiar $& to refer to whatever matched by the whole expression.

                const std::string rep("\\$&");
                std::string result = regex_replace(url_to_escape, esc, rep,
                                                   boost::match_default | boost::format_perl);
                

                这篇关于如何转义字符串以在 Boost Regex 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何链接到动态提升库? 下一篇:链接 boost 日志教程时的链接器错误(未定义的参考)

                相关文章

                • <bdo id='kKhDv'></bdo><ul id='kKhDv'></ul>

                  <tfoot id='kKhDv'></tfoot>

                  <small id='kKhDv'></small><noframes id='kKhDv'>

                  1. <legend id='kKhDv'><style id='kKhDv'><dir id='kKhDv'><q id='kKhDv'></q></dir></style></legend>
                    <i id='kKhDv'><tr id='kKhDv'><dt id='kKhDv'><q id='kKhDv'><span id='kKhDv'><b id='kKhDv'><form id='kKhDv'><ins id='kKhDv'></ins><ul id='kKhDv'></ul><sub id='kKhDv'></sub></form><legend id='kKhDv'></legend><bdo id='kKhDv'><pre id='kKhDv'><center id='kKhDv'></center></pre></bdo></b><th id='kKhDv'></th></span></q></dt></tr></i><div id='kKhDv'><tfoot id='kKhDv'></tfoot><dl id='kKhDv'><fieldset id='kKhDv'></fieldset></dl></div>