in reply to Wildcard?

Simple regexp.. if ($string=~ /^R..$/i) { #bleah }. The i at the end makes it case-insensitive, if you want it case sensitive (ie. matches 'Ray' but not 'ray') then remove it.

Update: Thanks Juerd for the correction below, although realised we're both wrong!

The following will match any three letter string, starting with a 'r' and optionally ending with a newline... if ($string =~ /^R[A-Z][A-Z]\n?$/i) { # bLEEEA }

Again this is case insensitive, to make it sensitive now though you need to change the A-Z bits into which case you want.

Replies are listed 'Best First'.
Re: Re: Wildcard?
by Juerd (Abbot) on May 30, 2002 at 12:36 UTC

    /^R..$/

    That will match ray but also ray\n, but not ra\n.

    /^R..\z/s
    \z is end of string, /s makes . match \n.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      If you've got the /s switch on though this'll happily match R\n\n\n, won't it?

      Update: Oops, did wrong number of \n's. R\n\n does seem to pass this match, unless the code below is wrong..

      #!/usr/bin/perl -w use strict; my @tests = ( "R\n\n", # Passes "Roo", # Passes "roo", # Fails "Roo\n", # Fails ); foreach (@tests) { if (/^R..\z$/s) { print "$_ passes\n"; } else { print "$_ fails\n"; } }

        If you've got the /s switch on though this'll happily match R\n\n\n, won't it?

        No, because I also put \z there instead of $. perlre

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.