zotfile
zotfile copied to clipboard
How to use a "replace" regex in custom wildcards
Hi,
I'm playing with custom defined wildcard for the renaming and I'm having trouble understanding how the "replace" regex function works.
In the website (http://zotfile.com/#user-defined-wildcards) I found this explanation:
replace: Replaces matches of a pattern using regular expressions (%4). Zotfile uses the replace() function with the regular expression regex and replacement string replacement. The replacement string can include $n for the _n_th parenthesized sub-match string and other special replacement patterns (see replace() documentationn). The wildcard %4, for example, takes the date when an item was added (format 2012-02-18 02:31:37) and returns the reformatted date as 20120218.
Required parameters: regex, replacement
Optional parameters: flags (default "g")
However looking at the example there it doesn't seem like it matches that description:
{
"1": "publicationTitle",
"2": {
"default": "publicationTitle",
"book": "publisher",
"bookSection": "publisher"
},
"3": {
"field": "title",
"regex": "([\\w ,-]{1,50})[:\\.?!]?",
"group": 1
},
"4": {
"default": {
"field": "title",
"regex": "([\\w ,-]{1,10})[:\\.?!]?",
"group": 1,
"transform": "upperCase"
},
"journalArticle": "publicationTitle"
}
}
There is no call to the function "replace" that I can see, there is a parameter "regex" but not a "replacement" one. In the default wildcard config I can see the function "exec" being used, but there are not examples of "replace" either.
Can anyone help with the syntax and how to actually format a replace regex?
Thanks!
You're right, the documentation isn't correct (probably was at one point but the author has developed the underlying code further since I would guess).
If you read javascript you might find it useful to read from about line 250 onwards in https://github.com/jlegewie/zotfile/blob/master/chrome/content/zotfile/wildcards.js
By doing this I figured out that if I wanted to replace runs of everything in a title that wasn't a dash, A-Z, a-z or 0-9, with an underscore, I needed to use:
{
"1":
{
"field": "title",
"operations": [
{
"regex": "[^-A-Za-z0-9]+",
"function": "replace",
"replacement": "_"
}
]
}
}
or
{"1": {"field": "title", "operations": [{"regex": "[^-A-Za-z0-9]+", "function": "replace", "replacement": "_"}]}}
omitting the whitespace
in order to get that value into the wildcard %1.
The correct code for the description is:
{
"1": "publicationTitle",
"2": {
"default": "publicationTitle",
"book": "publisher",
"bookSection": "publisher"
},
"3": {
"field": "title",
"operations": [{
"function":"exec",
"regex": "([\\w ,-]{1,50})[:\\.?!]?",
"group": 1
}]
},
"4": {
"field":"dateAdded",
"operations": [{
"function": "replace",
"regex": "(\\d{4})-(\\d{2})-(\\d{2})(.*)",
"replacement": "$1$2$3",
"flags":"g"
}]
},
"5": {
"default": {
"field": "title",
"operations": [
{
"function":"replace",
"regex": "\\d",
"replacement": ""
},
{
"function": "exec",
"regex": "([\\w ,-]{1,10})[:\\.?!]?",
"group": 1
},
{
"function": "toUpperCase"
},
{
"function": "trim"
}
]
},
"journalArticle": "publicationTitle"
}
}
Source: https://web.archive.org/web/20150205230644/http://zotfile.com/