reverse a regexp substitution
Given a string transformation expressed as a regexp substitution, is there a convenient way to reverse that transformation, preferably in python?
For example, given the transformation
def f(x): return re.sub('foo((:?bar)?)', r'\1', x)
which transforms 'foobar'
to 'bar'
and 'foo'
to the empty string, I would like to obtain
def g(x): return re.sub('((:?bar)?)', r'foo\1', x)
which does the reverse, in the sense that
f(g(x)) == x
Obviously not all regexp substitutions are 1:1 mappings, but my wishful thinking is that they can all be reversed in the sense of obtaining one possible input value g(x)
that would yield a given output x
from the original substitution.
Wh开发者_C百科y would I want to do this? I'd like to generate URLs for arbitrary filesystem paths based on parsing AliasMatch
directives in an Apache config file.
Your example doesn't work; ('(bar)?', 'foo$1')
is not the reverse of ('foo(bar)?', '$1')
.
If you try it (dropping the group substitution for the moment):
import re
re.sub(r'(bar)?', 'foo', 'xyz')
you get fooxfooyfoozfoo
.
That's because (bar)?
matches the null string, and the null string exists at every possible location in the string.
I'd suggest that with problems like this in such a simple example, you might want to seek a different approach to whatever you're actually trying to do.
(I dropped the group above because that doesn't actually work in Python. If you do re.sub(r'(bar)?', r'foo\1', 'xyz')
, it'll fail with an exception if (bar)?
was skipped. You can fix this with ((bar)?)
, so the outer grouping is never omitted. This is incidental to your problem, though.)
Well, practically what happens in this example is:
xy? -> $1
y? -> x$1
I'm not entirely sure what you're after with this - more examples wouldn't hurt - but I think that general formula could be expanded to cover more at least simple cases.
精彩评论