当找到 perl 正则表达式的子字符串匹配时,如何提取行中的整个单词

how to extract the whole word in the line when there is a match of my substring found perl regular expressions

我正在从不同日期生成的 perl(Windows) 解析多个日志文件。 每个文件只有一行,其中包含如下模式:

记事本/version_number

在我从日志文件读取的当前行中,如果字符串记事本存在,我想提取 notepad/version_number转化为一个变量(包括/

有人可以帮我解决这个问题吗?谢谢。

foreach $file (@file_names)
{
    open(my $fh, '<:encoding(UTF-8)', $file)
    or die "Could not open file '$file' $!";

    while (my $row = <$fh>)
    {
        if($row =~  "notepad")
        {
            #here I want to extract the part of the line which I have highligted above(notepad/*version_number) into a variable.
        }
    }
}

以上是我脚本中的代码片段。希望这有帮助。

日志文件中的示例行:

02/13/2014 22:39:51:464227 some_text notepad/v1.10.12 some_text

我可能过于简化了您的任务,但我认为这就是您所需要的:

foreach $file (@file_names)
{
    open(my $fh, '<:encoding(UTF-8)', $file)
    or die "Could not open file '$file' $!";

    while (my $row = <$fh>)
    {
        my ($result) = $row =~ m|(notepad/v\d+\.\d+\.\d+)|;
        if ($result) {
           # we have a match            
        }
    }
}

您已经有一个正则表达式来查找匹配项 -- 我不会添加第二个;我只是将其扩展为一步完成文本和捕获。如果 $result 为空,那么您几乎可以知道匹配失败。如果不是,那么你有你的文本。

如果版本文本不是 vx.x.x,那么您当然需要调整正则表达式以支持变化。

-- 根据 OP 的评论进行编辑 --

"everything from notepad/ until the next immediate whitespace"

my ($result) = $row =~ m|(notepad/\S+)|;

匹配 "notepad/",然后匹配所有非空白字符。