是否有元正则表达式语言或类?

时间:2016-01-12 10:14:15

标签: c++ regex

我发现标题和dfm文件中声明的控件之间存在差异,这可能是由于公式化程序的复制和粘贴造成的。

也许这些腐败的fiels负责使用CPP Builder XE7调试崩溃!

因此我想检查有关完整性的所有标头和dfm文件。

第一步是通过这种模式解析标题:

-eq 1

第一个文本是类名,第二个是标题中的变量名。

是否有一个带有元正则表达式的类,我可以像上面给出的模式那样简单地编写我想要的内容?

<TEXT> <Whitspaces> "*" <TEXT> <Whitespaces> ";"

我不想使用常见的正则表达式,每次都让我感到困惑:

<word> <white> * <word> <white> ;

2 个答案:

答案 0 :(得分:1)

您可以使用Boost Xpressive

  

正则表达式,可以写成字符串或表达式模板,并且可以使用无上下文语法的功能递归地相互引用它们。

我已广泛使用它们,并发现它们适用于(来自用户公会)的搜索:

sregex group, factor, term, expression;

group       = '(' >> by_ref(expression) >> ')';
factor      = +_d | group;
term        = factor >> *(('*' >> factor) | ('/' >> factor));
expression  = term >> *(('+' >> term) | ('-' >> term));

答案 1 :(得分:0)

今天的我心烦我的IDE一点点。不幸的是,你的提示都没有奏效。所以,我不得不再次承担全部责任,并编写了我自己的代码,以便再次找到内心的平静。 ^^我希望与正则表达式斗争的其他程序员发现此代码有用。它有点durty但至少:它有效!

HEADER

#include <string>

using namespace std;

class TReggae {
public:
  TReggae();
  TReggae& white  (void);
  TReggae& word   (string& strVar);
  TReggae& text   (string strVar);

public:
 int      iPos;
 string   str;
 bool     boFound;
};

实施

TReggae::TReggae() {
  iPos = 0;
  str  = "";
  boFound = false;
  }

TReggae& TReggae::white (void) {

  for (;iPos < str.length(); iPos++)
    if (!isspace(str[iPos]))
      break;

  boFound = true;

  return *this;
}

TReggae& TReggae::word (string& strVar) {

  strVar = "";
  for (;iPos < str.length(); iPos++) {
    if (!isalnum(str[iPos]))
      break;

    strVar += str[iPos];
    boFound = true;
    }

  if (strVar.length() == 0)
    throw -1;

  return *this;
}

TReggae& TReggae::text (string strVar) {

  if (strVar.length() == 0)    // assert
    throw -2;

  for (int i=0;i<strVar.length(); i++) {
    int iPosTmp = iPos + i;
    if (iPosTmp > str.length())
      throw -3;

    if (strVar[i] != str[iPosTmp])
      throw -4;
    }

  iPos += strVar.length();
  boFound = true;

  return *this;
}

TEST

void __fastcall TForm3::cxButton1Click(TObject *Sender)
{
  string  strClassName,
          strVarName;

  TReggae Reggea;
  Reggea.str = "class TMyClass { "
               "public: "
               "  TClassName1 * ptrVarName1; "
               "  TClassName2 * ptrVarName2 ;"
               "};";

  for (int i=0;i<Reggea.str.length();i++) {
    try {
      Reggea.iPos = i;

      Reggea.white ()
            .word  (strClassName)
            .white ()
            .text  ("*")
            .white ()
            .word  (strVarName)
            .white ()
            .text  (";");

      wostringstream os;
      if (Reggea.boFound) {
        os << "Found: "
           << Reggea.iPos << " "
           << strClassName.c_str() << " "
           << strVarName.c_str();
        cxMemo1->Lines->Add(os.str().c_str());

        i = Reggea.iPos;
        }
      }
    catch(...) {
      // cxMemo1->Lines->Add("Exception: No match!");
      }
    }

输出

Found: 53 TClassName1 ptrVarName1
Found: 83 TClassName2 ptrVarName2