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".

;-)

In reply to Re: How to use a negative backreference in regex? by shmem
in thread How to use a negative backreference in regex? by larryk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.