Quote:
Originally Posted by FandomWitch
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>[ ]*(&|/)[ ]*)(?P<first>Rose Tyler)(?P<post>.*)$=>\g<first>\g<delim>\g<pre>\g<post>
- This assumes that you know what delimiters can exist between ships. [ ]*(&|/)[ ]* should be pretty universal.
- 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.
- As the comment says, the regexp assumes a delimiter before the desired first name, otherwise it's already first and no change is needed.
- 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.
- Rearrange to be first, delim, pre, then post.
- pre is assumed to always exist, but post could be empty. If it's not, it starts with a delimiter.
- 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>[ ]*(&|/)[ ]*)(?P<first>Rose Tyler)(?P<post>.*)$=>\g<first>\g<delim>\g<pre>\g<post>
ships=>^(?P<pre>.*?)(?P<delim>[ ]*(&|/)[ ]*)(?P<first>Jack Harkness)(?P<post>.*)$=>\g<first>\g<delim>\g<pre>\g<post>
# Jack ends up before Rose when both appear.