Use field markers in :regex for citation key generator

So far I have been using the [authorsAlpha][shortYear] field markers for generating my citation keys. Working with LaTeX this is equivalent to the bibtex “alpha” style.
Recently I have decided to finally work with biblatex (with biber backend) instead of bibtex.
The biblatex “alphabetic” style differs from from bibtex’s “alpha” in that only 3 authors instead of 4 are considered before the key is abbreviated (e.g., BCPP20 will become Buc+20). Also the abbreviation differs for more than 5 authors (e.g., JBB+19 will become Joh+19).
I tried to continue working with the “alpha” style in JabRef but it can become quite confusing when the key in the library differs from what is shown in the compiled document. Therefore, I am trying to define a pattern for the key generator which fits to the biblatex “alphabetic”. However, I don’t see whether or how this is possible. The following questions arise:

  1. Can one use the predefined field markers (like [authorN]) in the :regex() modifier of the generator? Then I could just substitute the “[authorsAlpha]” part with “[author3]+” when it contains 4 letters.
  2. Is there some way to include conditionals in the key generator pattern?
  3. Do I miss another way of achieving my goal?

Best wishes,
Stefan

For someone who never uses RegEx the last hours were quite an interesting ride. I seem to have found a solution which is probably not elegant or efficient but works with what I know about JabRef.

  1. I chose the following pattern for the key generator: [authorsAlpha].[auth3]+[shortyear]
    That is, I generate a key containing two versions: (a) the “alpha” style from bibtex and (b) the first 3 letters from the first author name followed by “+”. The dot just serves as a separator and the year is appended.
  2. In Options > Preferences > Citation key generator I added the following regular expression in the left field of Replace (regular expression):
    (?:([^\.]{2,3}(?![^\.])\+?).*|.*\.(.*))([0-9]{2})$ Try it here
    In the by field I put: $1$2$3

The idea is to capture (a) if it is correct (in cases were it contains 1, 2 or 3 authors) or otherwise capture (b) in a capture group. A negative lookahead is used in the first capture group to prevent matching in the case of 4 or more authors. To exclusively capture one or the other, both capture groups are put into an alternative and each of the alternatives consumes the whole citation key except for the year. The year is captured in a third capture group.

Luckily JabRef is able to use references to capture groups in the by field. As either the first or the second capture group are empty all capture groups can just be concatenated to get the desired output.

I am still interested about the answers to my questions and if there is a more elegant/easier solution.

EDIT: Just realized that diacritics need to be considered. Thus the RegEx for the first capture group needs to be very relaxed (actually all chars except for the separating dot are allowed). This relaxation also covers the case of more than 4 authors (where [authorsAlpha] would, e.g., generate BCP+) as anything but a dot is now disallowed as the fourth char.

2 Likes