STL 消息的正则表达式过滤器 [英] Regex filter for STL messages

查看:39
本文介绍了STL 消息的正则表达式过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下 STL 错误:

Given the follow STL error:

./poly_power/poly_class.cpp:496: error: no matching function for call to ‘state_operator< polynomial< variable_term< polynomial< variable_term< std::basic_string<char, std::char_traits< char>, std::allocator< char> >, int> >, polynomial< variable_term<std::basic_string< char, std::char_traits< char>, std::allocator<char> >, int> > > >, polynomial<variable_term< polynomial< variable_term< std::basic_string< char, std::char_traits< char>, std::allocator< char> >, int> >, polynomial< variable_term< std::basic_string< char, std::char_traits< char>, std::allocator< char> >, int> > > > >::operation( std::pair< const std::basic_string< char, std::char_traits< char>, std::allocator< char> >, state_vector_term< polynomial< variable_term< polynomial< variable_term< std::basic_string< char, std::char_traits< char>, std::allocator< char> >, int> >, polynomial< variable_term< std::basic_string< char, std::char_traits< char>, std::allocator< char> >, int> > > >, polynomial< variable_term< polynomial< variable_term< std::basic_string< char, std::char_traits< char>, std::allocator< char> >, int> >, polynomial< variable_term< std::basic_string< char, std::char_traits< char>, std::allocator< char> >, int> > > > > >&)’

如何使用正则表达式将其简化为如下所示的内容:

how can I use a reg-ex expression to simplify it to something that looks like:

./poly_power/poly_class.cpp:496: error: no matching function for call to ‘state_operator<...>::operation( std::pair<...>&)’

即.将外部 <> 对中的所有内容转换为 ....我知道 STLFilt,一个执行类似操作的 perl 脚本,但我认为看看如何在纯正则表达式中完成这将是一种教学法.

ie. Convert everything within the outer pair of <> into .... I am aware of STLFilt, a perl script that does something similar, but I think it would be pedagogic to see how this could be done in pure regex.

奖金

对表达式进行参数化,使其在 <> 的第 n 层上工作.第一级将是上面的示例,而第二级将显示类似 state_operator, polynomial<...>>.

Paraitimze the expression so that it works on the nth level of <>. The 1st level would be example above, while the 2nd level would show something like state_operator<polynomial<...>, polynomial<...> >.

推荐答案

这里是一个几乎不可能与正则表达式匹配的不规则语法.

What you have here is an irregular grammar that is near-impossible to match with a regular expression.

我已经尝试了一些,嗯,它是可能的(前提是尖括号不在字符串的其他地方出现,而且它们是平衡的(每个左括号对应一个右括号),并且您可以为所涉及的嵌套定义上限):

I have experimented around a bit, and, well, it is possible (provided that angle brackets occur nowhere else in the string, that they are balanced (one closing bracket for each opening one), and that you can define an upper limit to the nesting involved):

匹配<...>(无嵌套):

<[^<>]*+>

匹配<...<...>...<...>...>(一层嵌套):

<[^<>]*+(?:<[^<>]*+>[^<>]*+)*+[^<>]*+>

最多匹配两层嵌套:

<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+>[^<>]*+)*+>[^<>]*+)*+[^<>]*+>

最多三个级别:

<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+>[^<>]*+)*+>[^<>]*+)*+>[^<>]*+)*+[^<>]*+>

最多四个级别:

<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+>[^<>]*+)*+>[^<>]*+)*+>[^<>]*+)*+>[^<>]*+)*+[^<>]*+>

最多五个级别:

<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+(?:<[^<>]*+>[^<>]*+)*+>[^<>]*+)*+>[^<>]*+)*+>[^<>]*+)*+>[^<>]*+)*+[^<>]*+>

为了获得加分,这里将最后一个分解成一个冗长的正则表达式,这样您就可以更容易地了解它的构造方式:

To get the bonus point, here's the last one broken up into a verbose regex, so you can see a bit easier how it is constructed:

<             # Match the first opening <
[^<>]*+       # Match any non-<>-characters possessively
  (?:         # Match the following zero or more times:
  <             # Match a <
  [^<>]*+       # etc. etc. etc.
    (?:
    <
    [^<>]*+
      (?:
      <
      [^<>]*+
        (?:
        <
        [^<>]*+
          (?:          # innermost level:
          <            # Match a <
          [^<>]*+      # Match any non-<> characters
          >            # Match a >
          [^<>]*+      # Match any non-<> characters
          )*+          # any number of times, possessively
        >            # then back one level: Match a >
        [^<>]*+      # etc. etc. etc.
        )*+
      >
      [^<>]*+
      )*+
    >
    [^<>]*+
    )*+
  >
  [^<>]*+
  )*+
[^<>]*+
>               # Match the final closing >

这不是太可怕了吗?

就您的示例而言,您似乎需要七层嵌套.如果你想要一个快速的正则表达式构造函数,它可能看起来像这样(在 Python 中):

Isn't this perfectly horrible?

For your example, it seems you need seven levels of nesting. If you want a quick regex constructor, it could look like this (in Python):

def innerregex(nesting):
    if nesting == 0:
        return ""
    else:
        return "(?:<[^<>]*+" + innerregex(nesting-1) + ">[^<>]*+)*+"

def makeregex(nesting):
    return "<[^<>]*+" + innerregex(nesting) + ">"

makeregex(2) 将返回一个可以正确匹配 <<<><>>> 的正则表达式.makeregex(7) 应该处理整个字符串,makeregex(6) 将只采用最里面的匹配,依此类推.

makeregex(2) will return a regex that can match <<<><>>> correctly. makeregex(7) should work on the entire string, makeregex(6) will only take the innermost matches, and so on.

这篇关于STL 消息的正则表达式过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆