当前位置: 首页 > 工具软件 > RE2 > 使用案例 >

re2 多模式使用例子

荣曾笑
2023-12-01
// Copyright 2008 The RE2 Authors.  All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include<re2/re2.h>
#include<re2/filtered_re2.h>
#include<cstdio>
#include<iostream>
#include<re2/set.h>


using namespace re2;
using namespace std;

int main(void) {
  RE2::Set s(RE2::DefaultOptions, RE2::UNANCHORED);

  s.Add("foo", NULL);
  if (s.Add("(", NULL) == -1) {
  	cout << "add patten  ( error" << endl;
  }
  s.Add("bar", NULL);
  s.Compile();

  s.Match("foobar", NULL);
  s.Match("fooba", NULL);
  s.Match("oobar", NULL);

  std::vector<int> v;
  s.Match("foobar", &v);
  cout << "v.size :" <<v.size() << endl;
  if(v[0] == 0) {
  	cout << "v[0] == 0\n";
  }

  if (v[1] ==1) {
  	cout << "v[1] ==1\n";
  }

  s.Match("fooba", &v);
  cout << "v.size :" << v.size() << endl;
  cout << "v[0] :" << v[0] <<endl;

  s.Match("oobar", &v);
  cout << "v.size :" << v.size() << endl;
  cout << "v[0] :" << v[0] <<endl;

}

编译:

g++ re2_test.cpp -o re2_test -lre2

运行:

./re2_test
结果:

re2/set.cc:49: Error parsing '(': missing ): (
add patten  ( error
v.size :2
v[0] == 0
v[1] ==1
v.size :1
v[0] :0
v.size :1
v[0] :1

如果有错误,在运行时会打印出来,s.Add("(", NULL),这个表达式语法有问题。

 类似资料: