Reputation: 895
I've devices a Java regex to perform below substitution
Pre-substitution | Post-substitution
=============================
GOSP_vhqjvfec | GOSP
INWMDN_10qkva | INWMDN
OS_INT_ihdqivmf0 | OS_INT
RSO15_1_%I_0gkuns | RSO15_1
AUDIT124_%I_qkbfn1 | AUDIT124
==============================
I've used this regex
regular exp -> (.*?)_%.*|(.*)_.*
substitution -> $1$2
I want to know if there is a better way to do it?
Upvotes: 1
Views: 77
Reputation: 785058
You can use this simpler regex:
str = str.replaceFirst("_(%\\S*|[^_\\s]+)\\b", "");
i.e. match an underscore followed by 2 alternations:
%
and 0 or more non-space charsThis all should be followed by a word boundary.
Upvotes: 3