4

I am doing the pattern matching as below. But I am not getting the proper output. Please suggest the correct code to get the correct output.

code

#! /usr/bin/perl -w

my $subString = "1.3.6.1.2.1.26.2.1.1.1.";
my $wholeString = "1.3.6.1.2.1.26.2.1.1.12.1";

 if ($wholeString =~ m/^$subString/)
{
print "matched string is : $&\n";
print "Wrong!!!!\n";
}
else
{
print "matched string is : $&\n";
print "Right!!!!!\n";
}

Actual Output: matched string is : 1.3.6.1.2.1.26.2.1.1.12 Wrong!!!!

Expected Output: matched string is : 1.3.6.1.2.1.26.2.1.1.1. Right!!!!!

What should I change in the code to get the expected output? Please

YVRAO
  • 43
  • 3
  • @YVRAO - See this: https://stackoverflow.com/a/7283308/6124824 – vkk05 Oct 06 '20 at 10:46
  • 2
    `index` is a function you should look into. – Shawn Oct 06 '20 at 10:48
  • It looks like you want the regex to NOT match the string. Making sure that it does not match is possible in many ways. So is swapping the strings "Wrong" and "Right". You need to explain the reason for which the regex should fail. Because the whole string is longer? Because the whole string has a digit after the last "1"? Those reasons require different regexes. The answers below are guessing at your intention, correctly or not is irrelevant, your question is unclear. – Yunnosch Oct 06 '20 at 11:01
  • In a regex '.' is used to match any character except a newline. – YVRAO Oct 06 '20 at 11:06
  • @YVRAO Yes, `.` matches `2`. You have a lot of `.` in your regex. – Ted Lyngmo Oct 06 '20 at 11:09
  • My intension is that the substring and wholestring are from two files say file_1 and file_2. When the entire substring matches with part of the wholestring I would like to print the line number in file_2. So, here I am looking for code changes to get the expected output. – YVRAO Oct 06 '20 at 11:18
  • @YVRAO Yes, you've gotten two answers already and they both work. – Ted Lyngmo Oct 06 '20 at 11:20
  • When you say "_When the entire substring matches with part of the wholestring_" it sounds like you want it to match _anywhere_ in `$wholeString`, but your regex starts with `^` which would only make it match if it _starts with_ `$subString`. – Ted Lyngmo Oct 06 '20 at 11:27
  • 1
    Yes, Ted Lyngmo. Thank you for your answers and comments. – YVRAO Oct 06 '20 at 11:45

2 Answers2

5

The dot has a special meaning in a regex - it means "match any character here". So the ".1." at the end of your substring is quite happy to match the ".12" in your test string.

To remove the special meaning of dot (and all other special characters) you can use the \Q escape sequence.

if ($wholeString =~ m/^\Q$subString/)

But (as has already been pointed out in the comments), a regex match is probably not the right tool here. You probably need the index() function.

if (index($wholeString, $subString) == 0)
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
3

. matches any character in a regex and it looks like you want to check if $wholeString starts with $subString without regex matching. This can be done using substr:

if(substr($wholeString, 0, length($subString)) eq $subString) {
    # $wholeString starts with $subString
} else {
    # $wholeString does not start with $subString
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108