Zotfile: customize journal abbreviation to rename files

I would like to customize journal abbreviation using the first letter of each word of the journal name.
I defined a new wildcard in extensions.zotfile.wildcards.user:
"z": {"field": "publicationTitle", "operations":[{"function":"exec","regex": "\\b[a-zA-Z]"}]}

And defined the renaming rules as:
{%z-}{%t}

However, it got me the first letter of the journal name, rather than the first letter of each word.
e.g., for “Journal of Risk and Uncertainty-Valuing mortality risk in China”,
I expect:
JoRaU-Valuing mortality risk in China
Yet I got:
J-Valuing mortality risk in China,

I would like to know how can I fix it.
Thank you very much.

Zotero version: 5.0.89
OS: Windows 10 OS Version 1909 (Build 18363.778)
  • This user-defined wildcard should do what you're looking for:
    {
    "1": {
    "field": "publicationTitle",
    "operations": [{
    "function": "replace",
    "regex": "\\B[a-zA-Z]",
    "replacement": ""
    },
    {
    "function": "replace",
    "regex": " ",
    "replacement": ""
    }
    ]
    }
    }


    You could also add {"function": "toLowerCase"} or {"function": "toUpperCase"} to the list of operations.

    Zotfile's documentation has links to Mozilla's explanation of the exec() and replace() functions. There, you can read: "The exec() method [...] [r]eturns a result array, or null." See also here: https://stackoverflow.com/a/5283091.

    You can test your regular expressions at https://regex101.com/. Minify or beautify your JSON at https://jsoncompare.com/.
  • Your solution works. Thank you so much, qqbb!
  • This is awesome!

    I've been trying to edit the wildcard but without success.

    I'm searching for the regular expression to abbreviate the journal using the first letter of each word, excluding transition words (such as "of", "and", etc).

    EX: "Journal of Volcanology and Geothermal Research" ---> return "JVGR"
    Currently, the solution provided by @qqbb returns "JoVaGR".

    I've tried testing the expression at https://regex101.com/ but failed miserably, as one cannot simply copy/paste the JSON field "regex" into the regular expression box and expect it to work.

    Thanks very much for any suggestion!
  • \\B[A-Z] should get you just the first letter of capitalized words
  • Many thanks @adamsmith for your quick reply!

    For some reason using \\B[A-Z] appears to fail, as it returns the full journal string, without spaces:

    "Journal of Volcanology and Geothermal Research" ---> "JournalofVolcanologyandGeothermalResearch"
  • oh sorry, misread the regex: you can just do [a-z] without any operator. That removes all lowercase letters and the second replace then removes the spaces
  • Works like a charm, thank you!

    In addition to your suggestion, I've added an operation to the JSON which removes everything in a journal title after a special character such as ":".

    EX:
    "Journal of Geophysical Research: Atmospheres" --> JGR
    "Journal of Volcanology and Geothermal Research" --> JVGR

    Here is the wildcard:


    {
    "z": {
    "field": "publicationTitle",
    "operations": [{
    "function": "replace",
    "regex": "\\s*[.:;?!|].*",
    "replacement": ""
    },
    {
    "function": "replace",
    "regex": "[a-z]",
    "replacement": ""
    },
    {
    "function": "replace",
    "regex": " ",
    "replacement": ""
    }
    ]
    }
    }


    Thanks again for your help!
  • if you want to be more efficient (not that it matters greatly) you could also just use a single replace for [^A-Z] which removes everything except capital letters. No need even for the second, space-removing replace.
  • Awesome, definitely more elegant and efficient!

    I've just tested it and it works perfectly, thank you.
Sign In or Register to comment.