in reply to Help with regex

Hi, you need to make the optional part optional:

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


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Help with regex
by bangor (Monk) on Sep 04, 2018 at 23:20 UTC
    Thanks 1nickt, that works perfectly.