shu*_*ham 3 regex perl substring
In the variable $hobbit I have stored value "Emulex LPe16000".
Now I need a regular expression to match the "LPe16000" part of the value after "Emulex".
Please ignore any syntax errors,I am a novice at perl..!
$hobbit="Emulex LPe16000"
if ($hobbit = ~m/Emulex ^\w+$/)
print "lol";
Run Code Online (Sandbox Code Playgroud)
该^方法断言字符串的开始。如果将其移到开头,则可以匹配,Emulex后跟一个空格,然后利用它\K来忘记匹配的内容。
然后匹配1个以上的单词字符\w+并断言字符串的结尾$
^Emulex \K\w+$
Run Code Online (Sandbox Code Playgroud)
如果要打印匹配项,您的代码可能如下所示:
my $hobbit="Emulex LPe16000";
if ($hobbit =~ m/^Emulex \K\w+$/) {
print $&;
}
Run Code Online (Sandbox Code Playgroud)