In addition to using the "complement character class" notation in the first suggestion above, there is also a thing called a "zero-width negative look-ahead assertion". (A bunch of "zero-width assertions", as well as character classes and everything else, are described in helpful detail in the
perlre man page.)
Note the following subtle difference between using a character-class vs. using a zero-width assertion:
#!/usr/bin/perl
use strict;
my $string1 = "don"; # should both of these match?
my $string2 = "donk"; # (you be the judge, and choose
# your regex accordingly)
my %regex = (
complem_char_class => qr/don[^t]/,
zwid_neg_lookahead => qr/don(?!t)/,
);
for my $regtyp ( sort keys %regex ) {
print "\n";
for ( $string1, $string2 ) {
my $result = ( /$regex{$regtyp}/ ) ? "succeeds" : "fails";
print "For $_ : match $result based on $regtyp\n";
}
}
The output of that little snippet shows that the character-class regex has to match
something (that is, there has to be a character in that position, and the character can be anything other than "t"), so the string "don" (with nothing after "n") won't match.
The "zero-width" operators allow you to state some condition that needs to be satisfied at a given position in the string, whether or not there happens to be a character present at that position.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.