Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

using ? and ?? in regex

by OzzyOsbourne (Chaplain)
on Apr 26, 2001 at 17:39 UTC ( [id://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.

-OzzyOsbourne

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.

    hdp.

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

    Cheers,

    Malk
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 01:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found