#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    45
    Rep Power
    12

    How to resolve this problem with Regular Expression?


    Hi there, i need a Regular expression for finding a line with this:

    Example

    PHP Code:
    ="WhatEverHere" 


    and cahnge it to :
    PHP Code:
    =\\"WhatEverHere\" 

    Notice that i need the same word inside the quotes, y only need the regex for adding the escape slashes to the line in front of the quotes.......

    NOTE: I dont know why is not shown the first slash??? so thats a forum error or protection!..-----
    Thanks!
    Last edited by Next_Gate; May 2nd, 2003 at 10:17 AM.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    South Africa
    Posts
    9
    Rep Power
    0
    Are you trying to do slash quoting of quotation marks.
    If this is in PHP:
    Maybe you could have a look at the string addslashes (string str) function
    You could also just use str_replace(search, replace, subject)

    If I'm missing the idea, just tell me.

    Here is the regex:
    for the matching:
    ="(.*?)"
    for the replacement
    =/"\1/"
    Last edited by sadrok; May 3rd, 2003 at 12:36 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    45
    Rep Power
    12
    im not trying to do it within the php program or script, i want to do it using a text editor. Im using Textpad that support RegEx...
  6. #4
  7. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    can you not use back references like \1 ?
    try the find bit like this:
    Code:
    [^=]\"
    and the replace bit:
    Code:
    \\\"
    Code:
    [^=]\"
    isn't as good as
    Code:
    =\"(.*?)\"
    because
    Code:
    [^=]\"
    will match any quote that doesn't have an equals infront of it. but if you can't use back refs..
    Last edited by balance; May 3rd, 2003 at 05:35 PM.
  8. #5
  9. Perl Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    May 2003
    Location
    the far end of town where the Grickle-grass grows
    Posts
    1,860
    Rep Power
    108
    With the note you added to your first post, do you just want all quotes escaped? That doesn't need a regex, just search for " and replace with \" If you don't want the quote following the = to be escaped, but all others should be, we'd need to know more about what kind of regex your editor supports. I use UltraEdit and it had it own odd version of regular expressions that I had to learn to use, but in recent versions, unix-style support has been added, which I'm much more familliar with.

    In perl, I'd use something like this:
    s/(?<!=)"/\\"/
    Perl supports a variety of zero width assertions (don't know how many are standard supported), positive and negative, look-ahead and look-behind. This one uses negative look-behind. (?<!pattern) matches so long as pattern does not match what comes immediately before it, in this case, an equals sign. Since it's zero-width, it doesn't actually consume the character in the match, and there's no need for explicit back references.
    Andrew - Perl (and VB.NET) Monkey

    Never underestimate the bandwidth of a hatchback full of tapes.

IMN logo majestic logo threadwatch logo seochat tools logo