View Single Post
Old 06-28-2023, 04:52 PM   #8569
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
Posts: 7,007
Karma: 4604635
Join Date: Dec 2011
Location: Midwest USA
Device: Kobo Clara Colour running KOReader
Quote:
Originally Posted by FandomWitch View Post
Okay I have searched the forum and haven't been able to find a solution. And my basic knowledge of regex and experimentation hasn't worked. Is there a way to have a specific character always be the first one listed in a ship. I would prefer to not have to use alphabetical, like really prefer not to.

So have it do something like this, but not have to write out every possible ship.

Code:
 ships=>9th Doctor & Rose Tyler & Jack Harkness=>Rose Tyler & 9th Doctor & Jack Harkness
 ships=>9th Doctor/Rose Tyler=>Rose Tyler/9th Doctor
 ships=>10th Doctor/Rose Tyler=>Rose Tyler/10th Doctor
Basically, move the character I want to the front and shift the rest or sort those alphabetically.
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.
JimmXinu is offline   Reply With Quote