1

I am comparing a string to a regular expression. If the string doesn’t match I want to know the location where the matching stopped in the string

For example if I am comparing my string (aaa) to regular expression (aab) than I want to know on what character the matching stopped. Here the second a.

I tried the following code, but I guess the variable $+[0] works only when strings are matched.

if($str!~/$reg/){
  print $+[0]
}

Here the variable is empty. So is it possible to do what I am looking for in perl?

  • 2
    You will need to provide a real example of the regex you are using to get a good answer to this. (There isn't a simple, universally applicable answer.) – ysth Dec 22 '13 at 07:21
  • 1
    Hi you may find this post useful [Position of first non matching character][1] [1]: http://stackoverflow.com/questions/7712282/perl-regex-position-of-first-nonmatching-character – coder hacker Dec 22 '13 at 07:57

2 Answers2

1

Perhaps the following will be helpful:

use strict;
use warnings;

my $string1 = 'aaaa';
my $string2 = 'aaba';
( $string1 ^ $string2 ) =~ /[^\x00]/;
print $+[0] if $+[0];

Output:

3

The result of the strings' XOR is a \x00 at each matching position. The regex matches the absence of that value, hence a non-matching character pair. In the case above, the two strings matched until their third characters.

Hope this helps!

Kenosis
  • 6,196
  • 1
  • 16
  • 16
  • 2
    Its a elegant solution. But it won't work if regexp uses operators like ?,* –  Dec 22 '13 at 08:00
0

I don't think negative matching can tell you which part of your string helped it to "succeed in not matching" without going deep into the re engine. A simpler solution would be to try to actually match and show where it failed.

Assuming that you want to match from left to right (as in your example), try something like this:

until($str =~ /$reg/) {
   $fail = substr $reg,-1; 
   $reg  = substr $reg,0,-1;
}
print "$reg\t$fail\n";

Of course, this could be simplified but you get the idea.

igelkott
  • 1,287
  • 8
  • 9