perl: regex for matching after an optional character -


i need take string can have 1 of 4 formats:

  1. html
  2. text
  3. attachment
  4. 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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -