简体   繁体   English

使用 Perl 打印整个字符串,直到出现子字符串匹配

[英]Print the entire string until a substring match occurs using Perl

I am looking for a quick fix for this, as I don't really specialise in perl/regex.我正在为此寻找快速解决方案,因为我并不真正专注于 perl/regex。

I have a string as follows ;我有一个字符串如下;

abc_def0_ghi4_jkl_mno_pqr_123456.log some_extra_messages and random garbage abc_def0_ghi4_jkl_mno_pqr_123456.log some_extra_messages 和随机垃圾

and I want a small perl script (not one-liner command) that will search for the .log file name, as well as remove the numbers at the end of the file.我想要一个小的 perl 脚本(不是单行命令),它将搜索 .log 文件名,以及删除文件末尾的数字。 So that the output looks like所以输出看起来像

abc_def0_ghi4_jkl_mno_pqr abc_def0_ghi4_jkl_mno_pqr

Any suggestions?有什么建议?

A quick (and maybe dirty) answer based on the few information you provided:根据您提供的少量信息快速(也可能是肮脏的)答案:

use strict;
use warnings;

my $str = "abc_def0_ghi4_jkl_mno_pqr_123456.log some_extra_messages and random garbage";

my ($name) = ( $str =~ /^(\w+)_\d+\.log/);
print $name, "\n";

This assumes that the name is located at the beginning of the script and contains only word characters (letters, numbers, underscore).这假定名称位于脚本的开头并且仅包含单词字符(字母、数字、下划线)。 It will capture everything until it reaches a sequence of numbers preceded by an underscore and followed by .log .它将捕获所有内容,直到它到达以下划线开头并后跟.log的数字序列。

It looks like you want to output everything up to, but not including, the underscore before the log file name.看起来您想输出所有内容,但不包括日志文件名前的下划线。 Is that correct?那是对的吗? You need to be very specific about how the data is structured;您需要非常具体地了解数据的结构; otherwise, we are having to guess.否则,我们不得不猜测。

Here's a short script that takes your example input and produces your example output:这是一个简短的脚本,它接受您的示例输入并生成您的示例输出:

use strict;
use warnings;

my $str = 'abc_def0_ghi4_jkl_mno_pqr_123456.log some_extra_messages and random garbage';

s/_[^_]+\.log.*// for $str;

print $str, "\n";

The s/_[^_]+\\.log.*// substitution matches: s/_[^_]+\\.log.*//替换匹配:

  • an underscore下划线
  • followed by one or more non-underscores后跟一个或多个非下划线
  • followed by the characters .log后跟字符.log
  • followed by any additional characters to the end of line后跟任何附加字符到行尾

The matched text is replaced with an empty string (deleted), and the remaining text is printed.匹配的文本被替换为空字符串(已删除),并打印剩余的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM