kyusan93
kyusan93

Reputation: 422

Perl regex in a variable string

I'm new to perl with regex.

I'm trying to have a string of oid 1.3.6.1.2.1.4.22.1.2.*.192.168.1.1, but I'm not sure how to do it. I tried the below, but it is getting error which is saying not able to recognize the oid.

my $matchanyoid = "/(\d+)$/";
my $dot1dTpFdbAddress = '1.3.6.1.2.1.4.22.1.2.',$matchanyoid,'\.',$srcip;

Upvotes: 0

Views: 175

Answers (1)

choroba
choroba

Reputation: 241808

Comma is not a concatenation operator, dot is:

my $dot1dTpFdbAddress = '1.3.6.1.2.1.4.22.1.2.' . $matchanyoid . '\.' . $srcip;

If you are trying to build a regular expression, note that the first several dots are not backslashed, so they can match anything. To avoid lots of backslashes, you can use the \Q ... \E construct:

my $matchanyoid = '(\d+)';
my $srcip = 12;
my $regex = qr/\Q1.3.6.1.2.1.4.22.1.2.\E$matchanyoid\.$srcip/;
print '1.3.6.1.2.1.4.22.1.2.123.12' =~ $regex;

Upvotes: 1

Related Questions