Web Analytics Made Easy -
StatCounter regExp fun - CodingForum

Announcement

Collapse
No announcement yet.

regExp fun

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • regExp fun

    Hey

    I'm storing a shopping basket in a session string.

    It looks, for instance, like this:

    1-1_9-1_13-1

    Which follows the format:

    ID1-Quantity1_ID2-Quantity2_ID3-Quantity3 etc etc etc...

    That make sense? Good. Right: I'm trying to write a "remove item" function that, passed an ID number, will do a regExp search & replace; replacing the ID-Quantity pair in question with an empty string.

    This is what I have so far:

    Code:
    function ereg_replace(strOriginalString, strPattern, strReplacement, varIgnoreCase)
      dim objRegExp : set objRegExp = new RegExp
      with objRegExp
        .Pattern = strPattern
        .IgnoreCase = varIgnoreCase
        .Global = True
      end with
      ereg_replace = objRegExp.replace(strOriginalString, strReplacement)
      set objRegExp = nothing
    end function
    
    strID=request("itemID")
    strBasket = session("basket")
    
    strBasket = ereg_replace(strBasket,"("&strID&"-[0-9]{1,3})", "", TRUE)
    ' remove any unattended underscores
    strBasket=replace(strBasket,"__","_")
    if right(strBasket, 1)="_" then
    	strBasket=left(strBasket, len(strBasket)-1)
    end if
    
    session("basket")=strBasket

    Now I assume that all that is wrong with this is my inexcusable excuse for a regular expression.

    What I want it to match, obviously () is a string consisting of the ID number of the item to be removed (strID) followed by a hyphen followed by a numeric value of between 1 and 3 characters in length.

    It doesn't.

    Any solutions? Is it just my dodgy regExp or am I overlooking something in the code?

    Thanks

  • #2
    I know I always champion regular expressions, but this is a clear candidate for the Split() function... i.e. Split(stringname,"_").

    I'd just assign an array variable to Split(stringname,"_") and loop through it...

    It seems you designed it for this?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

    Comment


    • #3
      Well... not really.

      You've got to split the basket up into individual ID-Quantity pairs, then loop through the resulting array, splitting those up into their respective IDs and quantities. Then if the ID matches the ID to remove, add it to the new basket string, writing it out a different way depending on whether you're at the beginning, middle or end of the new basket so that you don't get any unwanted underscores.

      That seems, to me, like more code to do the same job, and probably slower since you're doing all that string splitting and concatenation. If I used a regExp - my reasoning went - I could do the entire thing in one function call, plus a couple of replace()s to take care of the underscores.

      Am I wrong?

      Comment


      • #4
        "(" & strID & "-\d{1,3})" should match (and grab) the stringpart consisting the id, a dash, followed by 1,2 or 3 digits
        I am the luckiest man in the world

        Comment

        Working...
        X