I have a string which includes parenthesis with text inside the parenthesis. How do I remove the parenthesis with text at the end of the string while keeping the other words in string?
Input:
Potatoes Rice (Meat)
Output:
Potatoes Rice
My code:
#! /usr/bin/perl
use v5.10.0;
use warnings;
my $noparenthesis = "Potatoes Rice (Meat)";
$noparenthesis =~ s/^/$1/gi;
say $noparenthesis;
答案 0 :(得分:3)
#! /usr/bin/perl
use v5.10.0;
use warnings;
my $noparenthesis = "Potatoes Rice (Meat)";
$noparenthesis =~ s/\(.*$//g;
say $noparenthesis;
如果括号中还有其他词要保留,因为它们不在句子的末尾,则可以使用表达式:
$noparenthesis =~ s/\s*\([^()]+\)\s*$//g;
这只会删除字符串末尾的括号,可能的尾随空格以及它们之前的空格(因此,字符串中不会保留尾随空格)。由于匹配的括号中不允许使用(
和)
字符,因此,如果字符字符串中包含否定的字符类,则该字符将不与嵌套的括号匹配。