I have strings of this form:
A_B_CDEF_GHI
A_B_C_DEF_G_H_I
ABC_D_E_F_GHI
ABCDEFG_H_I
A_B_C
I need to convert those to the following:
AB_CDEF_GHI
ABC_DEF_GHI
ABC_DEF_GHI
ABCDEFG_HI
ABC
So the rules are:
(._){2,}should be converted toXXX_if it's not at the end of the string.- If
(_.){2,}occurs at the end of a string, it should be converted to_XXX. - If
(_.){2,}.is the entire string, all underscores should be removed.
I've gotten to (((.)_){2,}), which does match the first rule, but how can I replace it with the non-underscore characters it found?
The
pythontag is present because that's where the code is, and I know regex dialects depend on the language.