正则表达式匹配模式与(可选)内部相同的模式

时间:2012-09-03 06:40:32

标签: regex nested

所以我必须在代码中找到函数的所有调用,例如:_t(any)此外,还可以为这些调用提供参数:" _t(anything,_t(anything2))"对于这样的例子,我想只匹配_t()的最高调用有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

编程是为工作挑选合适的工具,而正则表达式实际上不是仅匹配函数顶级部分的正确工具。但是,编写一个程序以获取顶级部分是一个相当简单的任务,这是一个用C ++编写的程序,它将实现这一目标。它将解析存储在变量“str”中的字符串,并找到存储在“func”中的函数的顶级部分,因此在这种情况下它会找到“_t(”

#include <iostream>
#include <string>

using namespace std;

int main () {
    string str = "_t(another, anything)\nanything0 _t(anything, _t(anything2))\n_t(anything)\n_m(this, should, not, match)\n_t(anything1, anything2)";
    string func = "_t(";

    int pos = 0;
    int lastPos = 0;

    while (pos != -1) {
      pos = str.find (func, lastPos);

      if (pos == -1) break;

      int depth = 1;
      string tmp;

      pos += func.size ();
      while (depth > 0) {
        if (str[pos] == '(') {
          depth++;
        }
        else if (str[pos] == ')') {
          depth--;
        }

        if (depth != 0) {
          tmp += str[pos];
        }
        pos++;
      }

      cout << tmp << endl;

      lastPos = pos;
    }

    return 0;
}

给出以下输入(存储在字符串“str”中):

_t(another, anything)
anything0 _t(anything, _t(anything2))
_t(anything)
_m(this, should, not, match)
_t(anything1, anything2)

以下是输出:

another, anything
anything, _t(anything2)
anything
anything1, anything2

<小时/> 更新的 我确实认为,在处理嵌套项目时,正则表达式并不是最好的解决方案,但是,这可能适合您的需要:

_t\(([\w\d _\(\),]+)?\)

以下是PHP中的一个示例(正如您在另一条评论中所说的那样):

<?php 
$pattern = '/_t\(([\w\d _\(\),]+)?\)/';
$subject = '_t(another, anything)\nanything0 _t(anything, _t(anything2))\n_t(anything)\n_m(this, should, not, match)\n_t(anything1, anything2)';

preg_match_all ($pattern, $subject, $matches);

print_r ($matches);
?>

此输出如下:

Array
(
    [0] => Array
        (
            [0] => _t(another, anything)
            [1] => _t(anything, _t(anything2))
            [2] => _t(anything)
            [3] => _t(anything1, anything2)
        )

    [1] => Array
        (
            [0] => another, anything
            [1] => anything, _t(anything2)
            [2] => anything
            [3] => anything1, anything2
        )

)

这似乎与你所寻找的匹配,在$ matches [1]数组中。

答案 1 :(得分:0)

我不确定您是仅要匹配功能名称还是删除参数(包括非顶级调用)。无论如何here是替换正则表达式,输出:

_t()
_t()
_t()

_t(anything, _t(anything2))
_t(anything)
_t(anything1, anything2)

作为输入:

/(_t\().*(\))/\1\2/g