extracting all text after a dash, but only up to a second dash [Resolved]

Posted by Jist under Regular Expressions on 10/20/2012 | Points: 10 | Views : 2092 | Status : [Member] | Replies : 1

I'm a complete RegEx newbie, with very little knowledge yet.

I need to extract text from in between the first two '-' from a string. For example:

"first-second-third-fourth" should return "second" (without all the quote marks)

I accomplished this by creating: (.*?)-(.*?)-(.*)

$2 then indeed gives the required output "second"

Where it get's to complicated for me is when there is only 1 "-" in the string.

I then want everything behind that "-" returned.

But with my current regex e.g. "first-second" will return "first-second", while I there also want to get "second".

With my current knowledge of regex this is miles above my head.

The solution would be much appreciated!


Posted by: Jist on: 10/20/2012 [Member] Starter | Points: 25


I just got some answers from somebody on another forum, so this question may be ignored now.

He suggested: (?<=^[^-]+-)[^-]+


Jist, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response