perl: regex for matching after an optional character -
i need take string can have 1 of 4 formats:
html
text
attachment
email:[address]
i need regular expression correctly capture 2 things: $type
, html
, text
, attachment
, or email
, , $arg
, [address]
if $type
email
, , undef
otherwise. if $type
not email
, there should no matches @ all. i've written regex:
m/(html|email|text|attachment):?(.*)/;
which has problem match if there trailing text
, html
, or attachment
, , match if there no :
. so, instance, emailme@foo.com
give ("email", "me@foo.com")
. tried one:
m/(html)|(email):(.*)|(text)|(attachment)/;
which results in 5 groups. there way capture way want, no matches if there no colon after email
, or if there colon after else?
yes, can use branch reset feature: (?|...|...|...)
/(?|(html)|(email):(.*)|(text)|(attachment))/
in branch reset, capture groups of each alternative have same numbers.
to exclude, "html", "text", "attachment" followed else (including colon), need condition on right (anchor, lookahead or other). same thing beginning.
Comments
Post a Comment