Ignore Case in Glob() on Linux

Ignore case in glob() on Linux

Use case-insensitive regexes instead of glob patterns. fnmatch.translate generates a regex from a glob pattern, so

re.compile(fnmatch.translate(pattern), re.IGNORECASE)

gives you a case-insensitive version of a glob pattern as a compiled RE.

Keep in mind that, if the filesystem is hosted by a Linux box on a Unix-like filesystem, users will be able to create files foo, Foo and FOO in the same directory.

Case-insensitive Glob on zsh/bash

ZSH:

$ unsetopt CASE_GLOB

Or, if you don't want to enable case-insensitive globbing in general, you can activate it for only the varying part:

$ print -l (#i)(somelongstring)*

This will match any file that starts with "somelongstring" (in any combination of lower/upper case). The case-insensitive flag applies for everything between the parentheses and can be used multiple times. Read the manual zshexpn(1) for more information.

UPDATE
Almost forgot, you have to enable extendend globbing for this to work:

setopt extendedglob

Way to ignore case in iteration of glob

glob and the underlying fnmatch have no flag for case-independence, but you can use brackets:

for file in glob.iglob(os.path.join(dir, '*.[xX][lL][sS]')):

Ignore case when matching file name extensions with find

GNU versions of find have an -iname flag which enables case insensitive matching of file name globs,

find ./ -iname '*.jpg'

or if you are on a system without GNU utilities, use the bracket expressions to the glob

find ./ -name '*.[Jj][Pp][Gg]'

If you are interested in multiple name filters, just use the -o expression for including multiple name globs

find ./ \( -iname "*.jpg" -o -iname "*.jpeg" \)

PHP - Case insensitive File Search

I ultimately ended up fetching all images from the folder and checking for each sku in the image name array.

The following code solved my problem:

$path = $image_path ."/*.{jpg,png,gif}";
$all_images = glob($path, GLOB_BRACE);
$icount = count($all_images);
for($i = 0; $i < $icount; $i++)
{
$all_images[$i] = str_replace($image_path.'/', '', $all_images[$i]);
}

foreach($products as $product){
$matches = preg_grep ('/^'.$product['sku'].'(\w+)/i', $all_images);
}

Nevertheless, I would love to see case-insensitive glob implemented in future.



Related Topics



Leave a reply



Submit