in reply to Perl RegEx doubt

This will ensure that the string is exactly six characters, only consists of alpha-num, and matches the 5th char:

use warnings; use strict; my $str = 'xpy7pG'; my $char = 'p'; if ($str =~ /^\w{4}$char\w$/){ print "matched\n"; }

update: I lied. The above will also match an underscore (as \w matches [a-zA-Z0-9_]. If you don't want that, you can use this:

/^[0-9A-Za-z]{4}$char[0-9A-Za-z]$/

Replies are listed 'Best First'.
Re^2: Perl RegEx doubt
by AnomalousMonk (Archbishop) on Jun 07, 2016 at 14:50 UTC
    ... [0-9A-Za-z] ...

    There is also  [[:alnum:]] in the POSIX Character Classes (in perlrecharclass) which I tend to prefer as self-documenting.
    Also  \p{IsAlnum} if you've embraced the Dark Side of Unicode.


    Give a man a fish:  <%-{-{-{-<