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

    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Regular expression


    Hi All,

    I have a name as follows

    1. test-1-something
    2. tsome-somethingelse

    I wanted to write a regular expression to find the last dash (-) and remove until the dash. It should be like as below...

    1. something
    2. somethingelse

    could somebody tell me how I can do that.

    Thanks
  2. #2
  3. Autodidact
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Location
    Canada
    Posts
    741
    Rep Power
    833
    The good thing is that you don't even need to use a regular expression.
    Code:
    $exp = explode('-', 'test-1-something');
    echo $exp[count($exp) - 1]; // "something"
    Soldiers, from the height of these pyramids forty centuries look down upon you.
    ▪ Napoleon, on the eve of the Battle of the Pyramids
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0
    Thanks for your reply....

    But I should have to use regular expression as I can use RE in my application or even xpath. So I'm looking to have RE so that I can implement in my application.

    also there are bunch of stuff that i have with as I mentioned above..in other words
    the source can be like ..
    1. *-1-*
    2. *-*
    Thanks again.
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    A situation where the greedy dot-star is actually useful. Remove everything up to (and including) the last hyphen:
    Code:
    s/.*-//;
    (presented in Perl style, but hopefully you get the picture).
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0
    Thanks for your reply...

    One thing that I forgot to mention is I can only use certain verbs in my application. so it got to use "replace first" "replaceall".

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo