Skip to content

Add a regex replace escape for regnal numbers#1898

Open
tangledhelix wants to merge 1 commit into
DistributedProofreaders:masterfrom
tangledhelix:re-regnal-numbers
Open

Add a regex replace escape for regnal numbers#1898
tangledhelix wants to merge 1 commit into
DistributedProofreaders:masterfrom
tangledhelix:re-regnal-numbers

Conversation

@tangledhelix

@tangledhelix tangledhelix commented Jul 12, 2026

Copy link
Copy Markdown
Member

The existing \N escape converts a Roman number to an Arabic number. Given the books we work on, it's common to encounter Roman numbers which are regnal numbers, i.e., the number in a series of monarchs (kings, queens, popes).

This change implements a new escape (\M). I used M for Monarch, since \R (for Regnal) was taken already. With support for numbers in the range 1-99, it will take a Roman number as input (e.g. "II", "XIV", "XXIII") and output the regnal string prefixed by "the".

II    --> the Second
XIV   --> the Fourteenth
XXIII --> the Twenty-third

I'm not aware of any real-world regnal numbers higher than 23, but it was as easy to implement the 20s as it was to go through the 90s.

The existing \N escape converts a Roman number to an Arabic number.
Given the books we work on, it's common to encounter Roman numbers which
are regnal numbers, i.e., the number in a series of monarchs (kings,
queens, popes).

This change implements a new escape (\M). I used M for Monarch, since \R
(for Regnal) was taken already. With support for numbers in the range
1-99, it will take a Roman number as input (e.g. "II", "XIV", "XXIII")
and output the regnal string prefixed by "the".

    II    --> the Second
    XIV   --> the Fourteenth
    XXIII --> the Twenty-third

I'm not aware of any real-world regnal numbers higher than 23, but it
was as easy to implement the 20s as it was to go through the 90s.
@tangledhelix

Copy link
Copy Markdown
Member Author

Testing notes: I use this regular expression to find roman numbers.

(?<=[\s>-])(?!(?:x-ebookmaker|ill|civil|did|mild))([IVXLCDM]+|[ivxlcdm]+)(?![A-Za-z])(\.)?(?=[,\s<-])

Use this replace for testing:

\M\1\E

The most obvious use case in my mind is for abbreviations. An example replace for that case:

<abbr title="\M\1\E">\1\2</abbr>

@tangledhelix

Copy link
Copy Markdown
Member Author

Finally, a reminder: if this is merged, we'd want to update the wiki documentation to add \M, probably here.

@tangledhelix

tangledhelix commented Jul 12, 2026

Copy link
Copy Markdown
Member Author

Forgot this in the testing notes: that regex can cope with either upper or lowercase roman numbers, with or without a terminating ., and it should exclude some commonly encountered words like "civil", "did", etc, that are made up of entirely roman numeral characters. The pathological case is the word "I" which is impossible to exclude without more intelligence than a regex pattern can muster.

I'd be interested to hear of any other words to exclude that aren't already in the pattern, too, if you encounter any.

@windymilla

Copy link
Copy Markdown
Collaborator

Finally, a reminder: if this is merged, we'd want to update the wiki documentation to add \M, probably here.

Thanks for the link - I normally update the manual just before I do a release

@windymilla

Copy link
Copy Markdown
Collaborator

Testing notes: I use this regular expression to find roman numbers.

(?<=[\s>-])(?!(?:x-ebookmaker|ill|civil|did|mild))([IVXLCDM]+|[ivxlcdm]+)(?![A-Za-z])(\.)?(?=[,\s<-])

An alternative would be to trap Roman numeral construction more precisely than [IVXLCDM]+ with something like

(?=[MDCLXVI])(M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}))

Also, if you put (?i) at the start of one of our regexes it makes it case insensitive which sometimes simplifies things.

@windymilla windymilla left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code and behavior look good to me - thanks

@tangledhelix

Copy link
Copy Markdown
Member Author

An alternative would be to trap Roman numeral construction more precisely than [IVXLCDM]+ with something like

(?=[MDCLXVI])(M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}))

I'll tinker with this; I think it might need to be {0,4} for some of these, as you do run into the incorrectly formatted type. My current book has MCCLXXXX for 1290, for example, instead of MCCXC.

Dan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants