in reply to Wildcard search in a directory

"File glob" syntax is way different from regex syntax. If you want your script to handle file-glob patterns, use "glob()" as suggested in the first reply. If you want it to support the greater expressive power of regexes, then make it clear to script users that they should specify regexes instead of file glob patterns.

A few of the more prominent differences (assuming unix/linux file systems):

magic characterregex meaningglob meaning
. match any single character match a literal period
? match zero or one occurrence of the previous character or group match any single character
* match zero or more occurrences of the previous character or group match zero or more characters
+ match one or more occurrences of the previous character or group match a literal plus-sign
^ $ anchor regex to start/end of string match literal caret/dollar-sign
( ) capture a group of characters not supported

Square-bracketed character-classes (e.g. [a-z] work the same in both, but a pattern like  [a-z]* will mean very different things for regex vs. glob.

It would be possible (probably easy and maybe even useful) to write a "glob-to-regex" converter, to produce a regex pattern that behaves the same as a given glob pattern, but if you're just looking up file names in a directory, you already have the glob() function to handle that, so why bother with the extra coding?

Replies are listed 'Best First'.
Re^2: Wildcard search in a directory
by Anonymous Monk on Jul 05, 2007 at 18:35 UTC

    Thanks for the responses.

    I think glob() would suit my requirement better than regex. Will update after some more testing