2

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";

zdim
  • 64,580
  • 5
  • 52
  • 81
shubham
  • 182
  • 1
  • 1
  • 8

2 Answers2

4

The ^ means asserting the start of the string. If you move it to the start you could match Emulex followed by a space and make use of \K to forget what was matched.

Then match 1+ word characters \w+ and assert the end of the string $

^Emulex \K\w+$

Regex demo | Perl demo

If you want to print the match, your code might look like:

my $hobbit="Emulex LPe16000";
if ($hobbit =~ m/^Emulex \K\w+$/) {
    print $&;
}
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
2

\K is much better way to do it, yet this expression might also work:

Emulex\s+([A-Z]+[a-z]+[0-9]+)

Test

use strict;

my $str = 'Emulex LPe16000';
my $regex = qr/Emulex\s+([A-Z]+[a-z]+[0-9]+)/mp;
my $subst = '$1';

my $result = $str =~ s/$regex/$subst/rg;

print $result\n";

The expression is explained on the top right panel of this demo, if you wish to explore further or modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.

Emma
  • 27,428
  • 11
  • 44
  • 69