I am working on a script that develops certain strings of alphanumeric characters, separated by a dash -. I need to test the string to see if there are any sets of characters (the characters that lie in between the dashes) that are the same. If they are, I need to consolidate them. The repeating chars would always occur at the front in my case.
Examples:
KRS-KRS-454-L
would become:
KRS-454-L
DERP-DERP-545-P
would become:
DERP-545-P
This uses a positive lookahead
(?=...)to check for repeated strings.Note that
\walso contains the underscore. If you want to limit to alphanumeric characters only, use[a-zA-Z0-9].Also, I’ve anchored with
^as you’ve mentioned: “The repeating chars would always occur at the front […]”