Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to use a negative backreference in regex?

by shmem (Chancellor)
on Mar 02, 2009 at 23:03 UTC ( [id://747569]=note: print w/replies, xml ) Need Help??


in reply to How to use a negative backreference in regex?

P.S. ultimately, I'm trying to create a pattern that is able to say, "at some character position there either is or is not one of the preceeding characters," e.g. "test" =~ /^(.)([^\1])([^\1\2])\1$/

The following constructs such regexes. It takes a character sequence, and a file as input, and outputs the constructed pattern, then all the matching words in the file. Giving the characters numbers, these vertically aligned pairs match

otto letter character 1221 123324 123431564
so the constructed regexp for character would be
((\w)(?!\2)(\w)(?!\3|\2)(\w)(?!\4|\3|\2)(\w)\4\2(?!\5|\4|\3|\2)(\w)(?! +\6|\5|\4|\3|\2)(\w)\5)

Note that the back-references start with 2 because of the outer parens, which enclose $1 (or \1 inside the regexp).

#!/usr/bin/perl # match.pl use strict; my ($pat, $file) = @ARGV; my $p; { my (%s, %i); my $d = my $c = 1; # our regexp will be inside parens, so first back +ref is 2 $p = join ( "", map { if($s{$_}++){ "\\".$i{$_} } else{ $i{$_}=++$c; $c>$d+1 ? '(?!'.join('|',map{"\\".abs}-$c+1..-$d-1).")(\\w)" : + "(\\w)"; } } split//,$pat ); } print '(',$p,")\n"; open my $fh, '<', $file; my %s; while (<$fh>) { my @l = (); while (/\b($p)\b/g) { push @l, $1 unless $s{$1}++; } print join (", ",@l), $/ if @l; }

Try match.pl fusselkerl /usr/share/dict/words.

update: how would you specify a sequence to match a word composed of 15 different characters, which is 15 characters long? right: "dermatoglyphics". Or "1234567890abcde".

;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://747569]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-28 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found