0

I have to match a string in format Krait400.1C.28, when I store it in a variable:

$string_to_match = "Krait400.1C.28" foreach (@line) { if($_ =~ /$string_to_match/i) { print "found"; } else { print "notfound"; } } 

it doesn't match. But if I replace $string_to_match with "Krait400.28.1C" it matches. Also, there are other strings which do match and in the same format like xxxx.yy.1C, but I have problems with some of them like "Krait400.28.1C", it is acting really weird

I have counted characters, everything is same, but I don't know why it is not matching!

Please help me out!

17
  • 3
    You need to give us a sample of a line of text that should match but doesn't. Also, you should use /\Q$string_to_match/i to escape any metacharacters. In this case the dots in the pattern will match any single character in the object text, instead of matching dots. But this isn't the source of your problem, as it would make it match lines when it shouldn't and your problem is the reverse of that Commented Dec 29, 2015 at 22:30
  • 1
    There's a missing ; after the assignment. After adding the string to the @line array, I got "found" back. Commented Dec 29, 2015 at 22:42
  • 1
    It works for me: ideone.com/5SR4eY Commented Dec 29, 2015 at 22:44
  • 1
    @user: But do you understand that a pattern without the quotes would work fine as well? Commented Dec 29, 2015 at 22:52
  • 1
    @user Or maybe they did something like =~ /foo$string_to_match/. We can speculate all day about what the OP's code really is and why they (apparently) mis-copied it when composing this question, but that seems rather pointless. Commented Dec 29, 2015 at 23:11

1 Answer 1

1

Sorry guys, the bug wasnt in the regex, it was in a subroutine I called after the first element mismatches, and then it used to mismatch the rest. It was really hard to figure that part, as it was going into the loop where I perform regex and it wasnt matching there!

Sorry for all your troubles!

Thanks a lot though for your inputs!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.