perl -Mstrict -wlE ' my @str=("w7R9Kstc017510: split:","w7R9Kstc017510[1]: split:","w7R9Kstc017510[2]: split:"); for (@str) { say "$_ matches" if /(\w+)(?:\[\d+\])?: split:/ # here ^ } ' w7R9Kstc017510: split: matches w7R9Kstc017510[1]: split: matches w7R9Kstc017510[2]: split: matches