views:

78

answers:

1

I have a string, say r"a". I want to replace every r"a" with the string r"\1", but my regex engine does not understand this.

I have tried:

  • r"\1" -- crashes (can't match group 1 because there is no group 1)
  • r"\\1" -- crashes (not sure why)

Is this a limitation of my (proprietary) regex engine, or is it a general problem? Is there an elegant way of solving it? (I could e.g. replace "a" by "/1" and then StrReplace( "/", r"\" )... but that's not nice!)

+2  A: 

The correct way would be to use r"\\1" as a replace string. So if your proprietary regex engine/language chokes on a \\, you should fix this bug.

If you look at your example, you don't need a regex engine at all. But perhaps the example is simpler than the actual requirement...

Tim Pietzcker
Thanks. Unfortunately it is indeed rather more complicated than this. Nested StrReplaces, here I come!
katrielalex