Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

using ? and ?? in regex

by OzzyOsbourne (Chaplain)
on Apr 26, 2001 at 17:39 UTC ( #75782=perlquestion: print w/replies, xml ) Need Help??

OzzyOsbourne has asked for the wisdom of the Perl Monks concerning the following question:

I was using the following code to extract certain files with file::find. It works fine if I name individual files. I thought I'd get slick and try to make it shorter by using mp?? to replace mpeg, mp3, mp2, etc. and I replaced rmj, rmx, etc with rm?.

Problem is, now it doesn't find any of these files. What am I missing?

sub wanted { if (!("$File::Find::dir"=~/}/)&&(/\.asf$|\.mp??$|\.avi$|\.exe$|\.wav$|\.z +ip$|\.mov$|\.rm?$|\.wma$/i)){ print OUTFILE "$_\n"; print "$_\n"; }

Thanks, again.


Replies are listed 'Best First'.
Re: using ? and ?? in regex
by merlyn (Sage) on Apr 26, 2001 at 17:40 UTC
    ? in a regex doesn't mean "one character". It's a modifier. You want "." instead. So ".." to find any two characters.

    Regex syntax is not glob (filename wildcards) syntax.

    -- Randal L. Schwartz, Perl hacker

Re: using ? and ?? in regex
by hdp (Beadle) on Apr 26, 2001 at 17:41 UTC
    You're using ? in the shell wildcard sense to mean "any character"; unfortunately, that's not what it means in a regular expression. See perldoc perlre for a list of all the metacharacters. You probably want . instead.


Re: using ? and ?? in regex
by suaveant (Parson) on Apr 26, 2001 at 17:43 UTC
    because ? modifies the character before it... mp? matches m or mp... ?? matches non-greedily, so if mp is there... it will only match m unless it needs to match the p for later parts of the regex to match. .? will match a single char if it is there... .{0,2} will match 0 to 2 characters. .?.? would as well, but I think .{0,2} may be better.
                    - Ant
Re: using ? and ?? in regex
by Malkavian (Friar) on Apr 26, 2001 at 17:45 UTC
    Try mp\w+$, as the ? is the shell (*NIX) single char wildcard.
    The \w+ will most likely give you the behaviour you want (or \w{1,2} if you want either 1 or 2 chars.).


Re: using ? and ?? in regex
by diarmuid (Beadle) on Apr 26, 2001 at 17:53 UTC
    well ? is a modifier to your search so I'm not even sure what mp?? will match . mp? will match mp or m so I guess mp?? will match m or mp also.

    What you need is mp\w{1,2}. similarly for rmj use rm\w

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://75782]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-09-27 04:26 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (118 votes). Check out past polls.