【问题标题】:Parsing text File and extracting information in perl在 perl 中解析文本文件并提取信息
【发布时间】:2015-06-07 00:19:45
【问题描述】:

我有一个具有多种功能的代码

void fun_one()

{

tempa=10;

tempb=10;

}

void fun_two()

{

tempc=12;

tempd=13;

}

如果我输入变量名,我想要提取函数名的 perl 代码;

假设我搜索“tempa”,它应该将“fun_one”提取到一个变量中;

假设我搜索“tempd”,它应该将“fun_two”提取到一个变量中;

我已经提取了行号

open my $code, '<', 'code.txt'  ;

my $keyword = "tempa";

my $regex = qr|\b($keyword)\b|;


while ($code>)

{

    while (/$regex/g)

    {

        print "$.\n";

    }

}

输出:3 我需要一些逻辑来提取函数名称。

提前致谢

【问题讨论】:

  • 打开我的 $code, ') { while (/$regex/g) { print "$.\n"; } }
  • 请使用编辑功能添加您的问题。

标签: perl parsing text


【解决方案1】:

您可能需要一个解析器,而不仅仅是正则表达式。

但是……

sub find_function_using_variable {
  my $variable = shift;
  my $last_func="";
  while (<>) {
    $last_func = $1 if m/void (\w+)\(\)/;
    print $last_func."\n" if m/\Q$variable\E/;
  }
}

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 2015-04-19
    • 2013-08-17
    • 2020-07-21
    • 1970-01-01
    • 2021-09-23
    • 2022-11-18
    • 2012-10-09
    • 1970-01-01
    相关资源
    最近更新 更多