/ ^ # from the start ( # match ( [\w\d\-\_]+ ) # one or more of [class] [:,] # followed by a delimiter | # or match # nothing # nothing ) # then ( .* ) # match zero or more characters $ # .. until we reach end of string /x #### perl -wle 'print "ok" if undef =~ /^(([\w\d\-\_]+)[:,]|)(.*)$/'