I am really stuck with the following regex problem:
I want to remove the last piece of a string, but only if the '-' is more then once occurring in the string.
Example:
BOL-83846-M/L -> Should match -M/L and remove it
B0L-026O1 -> Should not match
D&F-176954 -> Should not match
BOL-04134-58/60 -> Should match -58/60 and remove it
BOL-5068-4 - 6 jaar -> Should match -4 - 6 jaar and remove it (maybe in multiple search/replace steps)
It would be no problem if the regex needs two (or more) steps to remove it. Now I have
[^-]*$
But in sublime it matches B0L-026O1 and D&F-176954
Need your help please
CodePudding user response:
You can match the first -
in a capture group, and then match the second -
till the end of the string to remove it.
In the replacement use capture group 1.
^([^-\n]*-[^-\n]*)-.*$
^
Start of string(
Capture group 1[^-\n]*-[^-\n]*
Match the first-
between chars other than-
(or a newline if you don't want to cross lines)
)
Capture group 1-.*$
Match the second-
and the rest of the line