我要翻译的数据串如下:
(wlc-nyhy30-a) *
我需要移除开头和结尾精确匹配的括号
) *
。
我的最终字符串应该是:
wlc-nyhy30-a
我可以很容易地去掉开头的 (
,像下面这样,但我非常难以让Perl匹配并删除行末的 ) *
这是有效的第一部分:
$output{'prompt'} =~ s/^\(//;
感谢任何帮助。提前致谢。
s/^\((.*)\)\s\*$/$1/
解释:
^ beginning of the string
\( opening parenthese - needs to be escaped with \
(.*) ...a capturing group...
\) closing parenthese - needs to be escaped with \
\s a single space
\* a star - needs to be escaped
$ end of the string
这将整个字符串进行匹配,因此它将适用于您提供的示例字符串((wlc-nyhy30-a) *
),同时也不会影响其他字符串,例如(wlc-nyhy30-a) a
。
演示:
my $string = "(wlc-nyhy30-a) *";
$string =~ s/^\((.*)\)\s\*$/$1/
print $string, "\n";
产生:
wlc-nyhy30-a
") *)
在目标字符串中的哪里? - user12097764)*
”一样。 - ikegami