View Single Post
Old 06-30-2023, 05:56 AM   #8575
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 11,000
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quote:
Originally Posted by JimmXinu View Post
This feels really familiar. I'm sure I've done something similar before, but I don't immediately find it either.

Detecting and moving one name out of a list to the front isn't difficult. Doing it while preserving the delimiters between names when the target name can be first, middle or last is a little trickier.

Here's what I've come up with using named regexp grouping for clarity:
Code:
 # assume leading delim, otherwise, already first
 ships=>^(?P<pre>.*?)(?P<delim>[ ]*(&amp;|/)[ ]*)(?P<first>Rose Tyler)(?P<post>.*)$=>\g<first>\g<delim>\g<pre>\g<post>
  1. This assumes that you know what delimiters can exist between ships. [ ]*(&amp;|/)[ ]* should be pretty universal.
  2. This also assumes that each ship will only have one kind of delimiter--eg, you won't have A & B / C in one ships entry.
  3. As the comment says, the regexp assumes a delimiter before the desired first name, otherwise it's already first and no change is needed.
  4. Everything before the first delimiter and the desired first name is grouped in pre, the delimiter in delim, desired first name in first, and everything after (including delimiters) in post.
  5. Rearrange to be first, delim, pre, then post.
  6. pre is assumed to always exist, but post could be empty. If it's not, it starts with a delimiter.
  7. sort_ships is applied before replace_metadata, so you can do both if you want.

If you get the wild idea to apply more than one of these, remember that replace_metadata lines are applied in order, which in this case means the last line 'wins'. See INI-File wiki and add_to_keyword in particular if you try to get fancy with [defaults] vs different sites.

Example:
Code:
 ships=>^(?P<pre>.*?)(?P<delim>[ ]*(&amp;|/)[ ]*)(?P<first>Rose Tyler)(?P<post>.*)$=>\g<first>\g<delim>\g<pre>\g<post>
 ships=>^(?P<pre>.*?)(?P<delim>[ ]*(&amp;|/)[ ]*)(?P<first>Jack Harkness)(?P<post>.*)$=>\g<first>\g<delim>\g<pre>\g<post>
# Jack ends up before Rose when both appear.
This might be useful to add to MetadataManagement page
ownedbycats is online now   Reply With Quote