Skip to content

Instantly share code, notes, and snippets.

@CanOfBees
Created May 3, 2020 02:31
Show Gist options
  • Save CanOfBees/22ad37d408e18864e37a7065502d6525 to your computer and use it in GitHub Desktop.
Save CanOfBees/22ad37d408e18864e37a7065502d6525 to your computer and use it in GitHub Desktop.
i needed to regex and this was the least destructive way i could think to use 'em
xquery version "3.1";
(: analyze-string example :)
declare variable $source external := "/home/bridger/rename-test/";
declare variable $target external := "/home/bridger/rename-target/";
declare variable $pav-text:= "(pav)(\.xml|\.mods.xml|\.txt)";
declare variable $pav-images := "(pav)(\d{1,})(\.tif)";
declare variable $cmmn-text:= "(\p{L}{1,})(\d{1,})(\.xml|\.mods\.xml|\.txt)";
declare variable $cmmn-image:= "(\p{L}{1,})(\d{1,})(\p{L}{1,})(\.tif)";
declare variable $cmmn-image-2:= "(\p{L}{1,})(\d{1,})(\.tif)";
declare variable $alphas :=
map:merge(
for $e in ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
count $count
return map:entry($e, string(format-number($count, '000')))
);
declare function local:pav-text(
$input as xs:string*
) as xs:string* {
let $analysis := analyze-string($input, $pav-text)
return $analysis/fn:match/fn:group[@nr='1'] || "_" ||
"001" ||
$analysis/fn:match/fn:group[@nr='2']
};
declare function local:pav-image(
$input as xs:string*
) as xs:string* {
let $analysis := analyze-string($input, $pav-images)
return $analysis/fn:match/fn:group[@nr='1'] || "_" ||
"001" || "_" ||
$analysis/fn:match/fn:group[@nr='2'] ||
$analysis/fn:match/fn:group[@nr='3']
};
declare function local:text-name(
$input as xs:string*
) as xs:string* {
let $analysis := analyze-string($input, $cmmn-text)
return $analysis/fn:match/fn:group[@nr='1'] || "_" ||
$analysis/fn:match/fn:group[@nr='2'] ||
$analysis/fn:match/fn:group[@nr='3']
};
declare function local:image-name(
$input as xs:string*
) as xs:string* {
let $analysis := analyze-string($input, $cmmn-image)
return $analysis/fn:match/fn:group[@nr='1'] || "_" ||
$analysis/fn:match/fn:group[@nr='2'] || "_" ||
$alphas?($analysis/fn:match/fn:group[@nr='3']) ||
$analysis/fn:match/fn:group[@nr='4']
};
declare function local:image-name-2(
$input as xs:string*
) as xs:string* {
let $analysis := analyze-string($input, $cmmn-image-2)
return $analysis/fn:match/fn:group[@nr='1'] || "_" ||
$analysis/fn:match/fn:group[@nr='2'] ||
$analysis/fn:match/fn:group[@nr='3']
};
(:let $in := ("gc023.xml", "gc023.mods.xml", "gc023.txt", "gc023a.tif", "gc023b.tif", "GC023c.TIF", "counter-thing", "pav.xml", "pav.mods.xml", "pav.txt", "pav001.tif", "pav002.tif", "PAV003.TIF"):)
for $item in file:list($source)
let $i := lower-case($item)
return(
if (matches($i, $cmmn-image-2))
then file:copy($source || $item, $target || local:image-name-2($i))
else if (matches($i, $pav-text))
then file:copy($source || $item, $target || local:pav-text($i))
else if (matches($i, $pav-images))
then file:copy($source || $item, $target || local:pav-image($i))
else if (matches($i, $cmmn-text))
then file:copy($source || $item, $target || local:text-name($i))
else if (matches($i, $cmmn-image))
then file:copy($source || $item, $target || local:image-name($i))
else "no match, sorry: " || $i
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment