Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl RegExp:当存在特定字符串时,删除部分字符串_Regex_Perl - Fatal编程技术网

Perl RegExp:当存在特定字符串时,删除部分字符串

Perl 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

如果字符串包含土豆或桃子,如何应用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 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