I don't think this is what you want, and I'm not sure you would want to do what you 
want if you could do it.

Assuming that "OH" will always be the same length, you can use a look-behind regex 
like:

CODE
====
#!/usr/bin/perl -w
use strict;
my $str = "OH: If I was an Osc:ar Mayer Wiener, tha:t is what I:d Truly like to b:e";

$str =~ s/(?<!^.{2}):/\\:/g;
print $str;

DOCS
====
(?<!pattern)
    A zero-width negative look-behind assertion. For example /(?<!bar)foo/ matches any 
occurrence of "foo" that does not follow "bar". Works only for fixed-width look-behind.

REGEX BREAKDOWN
===============
s/       substitute by matching:
  (?<!   make sure whatever we match isn't preceeded by
    ^    start of line
    .{2} any two non-newline characters
  )
:        given the above, try to match a colon
/\\:/    replace with \:
g;       globally (repeat through string)

I have no idea about efficiency with this.  But if you do find a regex to match a 
variable length "look-behind" it probably won't be as efficient as doing it in two 
lines.

Another way of thinking of this might be that you are matching delimited text with a 
substitution, where the delimiters are ^[^:]+: and $.

Send us a few more examples, the problem may be even simpler.  I'm still thinking, but 
I've got to go.

---- "Steven W. Orr" <[EMAIL PROTECTED]> wrote:
>
> I have a string with (possibly) multiple colons in it,
> 
> OH: If I was an Osc:ar Mayer Wiener, tha:t is what I:d Truly like to b:e
> 
> I am looking for a single regex to turn all subsequent colons into \:
> 
> So given the above input I'd like to end up with
> 
> OH: If I was an Osc\:ar Mayer Wiener, tha\:t is what I\:d Truly like to b\:e
> 
> I can do it in two lines but I'd like to see if it can be done with just 
> one krafty regex.

---
Glen Peterson
Senior Software Engineer, Web Consultant
South Hamilton, MA USA




_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to