I have two very related questions:
I want to match a string pattern with a wildcard (i.e. containing one or more '*' or '?') and then form a replacement string with a second wildcard pattern. There the placeholders should refer to the same matched substring (As for instance in the DOS copy command)
Example:
pattern='*.txt'
andreplacement-pattern='*.doc'
: I wantaaa.txt
-->aaa.doc
andxx.txt.txt
-->xx.txt.doc
Ideally it would work with multiple, arbitrarily placed wildcards: e.g.,
pattern='*.*'
andreplacement-pattern='XX*.*'
.Of course one needs to apply some constraints (e.g. greedy strategy). Otherwise patterns such as
X*X*X
are not unique for stringXXXXXX
.or, alternatively, form a multi-match. That is I have one or more wildcard patterns each with the same number of wildcard characters. Each pattern is matched to one string but the wildcard characters should refer to the same matching text.
Example:
pattern1='*.txt'
andpattern2='*-suffix.txt
Should match the pairstring1='XX.txt'
andstring2='XX-suffix.txt'
but notstring1='XX.txt'
andstring2='YY-suffix.txt'
In contrast to the first this is a more well defined problem as it avoids the ambiguity problem but is perhaps quite similar.
I am sure there are algorithms for these tasks, however, I am unable to find anything useful.
The Python library has fnmatch
but this is does not support what I want to do.