Thread: Replace

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

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55

    Question Replace


    I need the following string to be replaced like so:

    Code:
    product[variation][0][name][0] //from
    product[variation][0][name][1] //to
    The "0" and "1" are not fixed. Both the needle and the replacement can be any digit. I am not very experienced with RegEx, but I came up with the following.

    Code:
    /([0-9].?)/   //seems to match both 0's
    /([0-9].?)(?=([0-9].?))/   //doesn't match anything
    I tried using a positive lookahead but this didn't work either. I am using this with Javascript's replace method.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Is the string just "product[variation][0][name][0]" or is there something else?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55
    Aside from the fact that the numbers change, that is it
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    So... that number is always followed by a "]" and then the end of the string, right?
    Code:
    /\[(\d+)\]$/
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55
    Actually I lied. The format is:

    Code:
    product[variation][0][option][0][name]
    product[variation][0][option][0][price]
    product[variation][0][option][0][cost]
    The last '0' would need to increment basically. Sorry for the confusion.
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Okay, then a number, ']', and possibly more '[' not-numbers ']' sequences, then the end of the string?
    Code:
    /\[(\d+)\](?:\[\D+\])*$/
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55
    Ok. That seems to find the last digit correctly, but it is replacing the brackets as well. So:

    Code:
    product[variation][0][option][0][name]
    Becomes:

    Code:
    product[variation][0][option]1
    It seems to replace everything after. If it helps, I am performing this replace via javascript using

    Code:
    string.replace(regex, replaceWith);
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55
    Anyone know how I can isolate just the last digit for replacement? The expression matches everything, and therefore replaces everything.
  16. #9
  17. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    So that's why I didn't have the ?:.
    Code:
    /\[(\d+)\](\[\D+\])$/
    Replace with "[whatever]$2".

    If you just have to have the digits only, there's the less efficient
    Code:
    /(?<=\[)\d+(?=\]\[\D+\]$)/
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55
    Ok that works for the replacement in PHP, but throws an error in Javascript. I'm assuming that is because Javascript doesn't support the look behind. ????

    Anyway around this? Or am I going to have to replace a larger portion of the string.
  20. #11
  21. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397

IMN logo majestic logo threadwatch logo seochat tools logo