I am trying to select in my regex only what is between <
and -
Regex
<(.*)-
STRING
<SM1-SRVNET-P:Sys
OUTPUT
SM1-SRVNET
Desired Exit
SM1
I am trying to select in my regex only what is between <
and -
Regex
<(.*)-
STRING
<SM1-SRVNET-P:Sys
OUTPUT
SM1-SRVNET
Desired Exit
SM1
You're using the "greedy" star operator, so the matching engine keeps matching .
to the end of the string and eventually backtracks to the second -
.
You want to have it try to match what follows (here -
) after every match of .
. You make it do this with the "lazy" star. It's lazy in the sense that it matches shortest string possible.
So try <(.*?)-
.
Another approach that's actually a bit more efficient is to match all but -
with the greedy star. That's <([^-]*)-
.