简体   繁体   English

Perl正则表达式替代最后一次出现

[英]Perl regex subsitute last occurrence

I have this input: 我有这个输入:

AB2.HYNN.KABCDSEG.L000.G0001V00
AB2.HYNN.GABCDSEG.L000.G0005V00

I would like to remove all which finish by GXXXXVXX in the string. 我想删除字符串中GXXXXVXX完成的所有内容。

When i use this code: 当我使用此代码时:

$result  =~ s/\.G.*V.*$//g;
print "$result \n";

The result is : 结果是:

AB2.HYNN.KABCDSEG.L000
AB2.HYNN

It seems each time the regex find ".G" it removes with blank . 似乎每次正则表达式找到".G"它都会用blank删除。 I don't understand. 我不明白

I would like to have this: 我想要这个:

AB2.HYNN.KABCDSEG.L000
AB2.HYNN.GABCDSEG.L000

How i can do this in regex ? 我如何在正则表达式中做到这一点?

$result =~ s/\.G\d+V\d+//g;

适用于给定的输入。

Update: 更新:

After talking in the comments, the final solution was: 在评论中进行讨论之后,最终的解决方案是:

s/\.G\w+V\w+$//;

In your regex: 在您的正则表达式中:

s/\.G.*V.*$//g;

those .* are greedy and will match as much as possible. 那些.*很贪心,将尽可能匹配。 The only requirement you have is that there must be a V after the .G somewhere, so it will truncate the string from the first .G it finds, as long as it is followed by a V . 您唯一的要求是,在.G之后的某处必须有一个V ,因此它将截断它找到的第一个.G的字符串,只要它后面跟随一个V There is no need for the /g modifier here, because any match that occurs will delete the rest of the string. 这里不需要/g修饰符,因为任何匹配都会删除字符串的其余部分。 Unless you have newlines, because . 除非您有换行符,否则是因为. does not match newlines without the /s modifier. 不带/s修饰符的换行符不匹配。

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

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