1

I have a Wordpad file from which I extract two strings and compare them. In this case they are both equal, but I cannot use the =~ expression to evaluate them.

if($pin_list =~ /$lvl_list/){ do something}

What I have tried in debug mode:

  1. Both strings are equal as evaluated by eq
  2. Both strings are equal as evaluated by ==
  3. Manually set another variable to same string and then perform if statement with new variable; if($pin_list =~ /$x/){do something}. This attempt was successful.
  4. Performed chomp(var) on both string vars several times and then ran code. FAILED
  5. Removed carriage return via $tst_pins =~ s/\n//g on both vars. FAILED
  6. Length of both vars is the same.
  7. Manual printed both vars and visually verified both strings are the same.

Anyone got any ideas? I suspect it is something that has to do with WordPad and perhaps a hidden char, but don't know how to track it down.


tchrist -> Good question. In this case the strings are equal, but that will not always be the case. Under normal conditions, I am simply looking for the one string to be a subset of another.


For those who may be interested. Problem solved.

I had a string that i 'joined' with '+'. So the string looked like the following:

"1+2+3+4+a+b+etc"

The '+' ended up being the problem. At the suggestion of a colleague I performed a substr and whittled away one of the strings down to the offending point. It occurred just after it captured the '+'. I then joined using a blank space instead of the '+', and everything works.

Using different characters other than the alphabet will have an impact that I still am at a loss as to explain why when everything else said it was equal.

Bret

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Bret
  • 11
  • 2
  • 1
    If they are `eq`, why are you not using that??? – tchrist Apr 14 '11 at 18:44
  • 2
    could you maybe create an answer of your solution instead of adding it in your question? That way, other people can more easily see the solution. You can even accept your own answer and collect some rep-points, if you're into that! :) – Bart Kiers Apr 14 '11 at 19:02

2 Answers2

4

The match operator (m// aka //) checks if the provided string is matched by the provided regex pattern, not if it is character for character equal to the provided regex pattern. If you want to build a regex pattern that will match a string exactly, use quotemeta.

This checks if $str1 is equal to $str2:

my $pat = quotemeta($str1);
$str2 =~ /^$pat\z/

quotemeta can also be called via \Q..\E.

$str1 =~ /^\Q$str2\E\z/

Of course, you could just use eq.

$str1 eq $str2
ikegami
  • 367,544
  • 15
  • 269
  • 518
3

+ and other characters have special meanings inside regular expressions, so just using $expression =~ /$some_arbitrary_string/ can get you into trouble.

If the question is whether one string is literally contained in another string, you can use index and not worry about all the rules for specifying regular expressions:

if (index($pin_list, $lvl_list) >= 0) {
    do_something;
}
mob
  • 117,087
  • 18
  • 149
  • 283
  • 2
    Assuming this is Perl, you can also use \Q and \E to automatically quote the variable's contents in the match expression. – Neil Apr 14 '11 at 19:08