Perl RegExp:当存在特定字符串时,删除部分字符串
如果字符串包含土豆或桃子,如何应用Perl RegExp删除字符串的第一部分 如果可能,不要使用If/else条件,而只使用RegExpPerl RegExp:当存在特定字符串时,删除部分字符串,regex,perl,Regex,Perl,如果字符串包含土豆或桃子,如何应用Perl RegExp删除字符串的第一部分 如果可能,不要使用If/else条件,而只使用RegExp Input: Apples Peaches Grapes Spinach Tomatoes Carrots Corn Potatoes Rice Output: Peaches Grapes Spinach Tomatoes Carrots Potatoes Rice 这是我的密码: #! /usr/bin/perl use v5.10.0; use
Input:
Apples Peaches Grapes
Spinach Tomatoes Carrots
Corn Potatoes Rice
Output:
Peaches Grapes
Spinach Tomatoes Carrots
Potatoes Rice
这是我的密码:
#! /usr/bin/perl
use v5.10.0;
use warnings;
$string1 = "Apples Peaches Grapes ";
$string2 = "Spinach Tomatoes Carrots";
$string3 = "Corn Potatoes Rice";
#Use RegExp to output strings with first word deleted
#if it includes either Peaches or Rice.
$string1 =~ s///;
$string2 =~ s///;
$string2 =~ s///;
say $string1;
say $string2;
say $string3;
您可以使用以下表达式:
^(?=.*\bPeaches\b|.*\bPotatoes\b)\S+\s
字符串的开头^
正向前瞻,确保字符串中存在(?=.*\b aches\b |.*\b totateos\b)
或桃子
子字符串土豆
匹配任何后跟空白的非空白字符\S+\S
Perl演示:
use feature qw(say);
$string1 = "Apples Peaches Grapes";
$string2 = "Spinach Tomatoes Carrots";
$string3 = "Corn Potatoes Rice";
$string1 =~ s/^(?=.*\bPeaches\b|.*\bPotatoes\b)\S+\s//;
$string2 =~ s/^(?=.*\bPeaches\b|.*\bPotatoes\b)\S+\s//;
$string2 =~ s/^(?=.*\bPeaches\b|.*\bPotatoes\b)\S+\s//;
say $string1;
say $string2;
say $string3;
印刷品:
Peaches Grapes
Spinach Tomatoes Carrots
Corn Potatoes Rice
您可以使用以下表达式:
^(?=.*\bPeaches\b|.*\bPotatoes\b)\S+\s
字符串的开头^
正向前瞻,确保字符串中存在(?=.*\b aches\b |.*\b totateos\b)
或桃子
子字符串土豆
匹配任何后跟空白的非空白字符\S+\S
Perl演示:
use feature qw(say);
$string1 = "Apples Peaches Grapes";
$string2 = "Spinach Tomatoes Carrots";
$string3 = "Corn Potatoes Rice";
$string1 =~ s/^(?=.*\bPeaches\b|.*\bPotatoes\b)\S+\s//;
$string2 =~ s/^(?=.*\bPeaches\b|.*\bPotatoes\b)\S+\s//;
$string2 =~ s/^(?=.*\bPeaches\b|.*\bPotatoes\b)\S+\s//;
say $string1;
say $string2;
say $string3;
印刷品:
Peaches Grapes
Spinach Tomatoes Carrots
Corn Potatoes Rice