0

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

4

2 回答 2

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] 数组中查找的内容相匹配。

于 2012-09-03T07:10:47.187 回答
0

我不确定您是只想匹配函数名称还是删除参数(包括非顶级调用)。无论如何,这里是一个替换正则表达式,它输出:

_t()
_t()
_t()

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

作为输入:

/(_t\().*(\))/\1\2/g
于 2012-09-03T06:51:39.300 回答