Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Regex to dereference

by gaal (Parson)
on Jan 18, 2007 at 13:30 UTC ( [id://595251]=note: print w/replies, xml ) Need Help??


in reply to Regex to dereference

Are you trying to find the length of the string? length is much better than your pos loop for that purpose.

Is the last line in your code meant to handle the case you're saying doesn't work in the main substitution? Well, sure the main one doesn't work, because /\\(.)/ doesn't match at the last character. It can't! $_ .= "\\" if /\\$/ is one way to do it -- no need for a length check at all.

By the way, "dereference" is usually understood to mean something else that what you are using it as here. The term you are looking for is probably "escape" or colloquially "backwhack".

Replies are listed 'Best First'.
Re^2: Regex to dereference
by Hena (Friar) on Jan 19, 2007 at 06:40 UTC
    The thing is that if there is only one '\' character, then it should be doubled, but if there is two then not. Basicly if there is even amount then no change, and odd, then change the last. So direct test on last char doesn't really work.

    But thanks for the term help. As all my perl (and most of my coding as well) is self-taught :).
      Ah, then you need what's called a zero-width negative look-behind.

      $str =~ s/(?<!\\)\\$/\\\\/; # match a backslash at the EOL # that isn't preceded by a backslash

      See perlre for more stuff like this.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://595251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-26 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found