lilydjwg / nvchecker

New version checker for software releases
MIT License
425 stars 68 forks source link

How how to transform a variable number of matches? #250

Closed alerque closed 6 months ago

alerque commented 6 months ago

I have no problem using from_patterm/to_pattern to rewrite either X_Y_Z→X.Y.Z or X_Y→X.Y, but I have an upstream source with a variable number of segments in their underscore-formatted tag scheme that I need to translate to dot separators.

I can match on it of course and transform it to something, but I can't figure out how to only include the trailing .\\3 if and only if there is a third section to match.

I can almost get away with stuffing a zero in the third segment:

from_pattern = '(\d+)_(\d+)_?(\d+)?'
to_pattern = '\1.\2.0\3'

But that actually falls afoul of version sorting rules for other reasons.

Is there a way to do simple substitution (e.g. replace _.) or to add a string to a backreference only if it is not empty?

lilydjwg commented 6 months ago

For your example:

from_pattern = "_"
to_pattern = "."
alerque commented 6 months ago

Oh. :facepalm:

Thanks.