Are you a RegEx bandido?

  • Thread starter Thread starter Hillbilly
  • Start date Start date
H

Hillbilly

Its not that I hate RegEx its that I have to relearn the same stuff every
couple of months because I forget what I last learned. Common eh? So... I'm
working on an expression to validate a MIME Type and I have a fairly decent
pattern set up but it needs some work...

<%--
MIME Type
match: type/name
match: type/name5
match: type/x-name
match: type/x-name-name
match: type/x-name+name
match: type/x-name_name
match: x-type/name

fail: type-/name
fail: type/name-
fail: type//name
fail: type_type/name
ValidationExpression="^([a-z]+-?[a-z]+)/([a-zA-Z0-9]+-?\+?\.?_?[a-zA-Z0-9]+)+$"
--%>
<asp:RegularExpressionValidator
ID="Validator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="Invalid MIME Type Format"
Display="Dynamic"
Font-Bold="true"
SetFocusOnError="true"
ValidationExpression="^([a-z]+-?[a-z]+)/([a-zA-Z0-9]+-?\+?\.?_?[a-zA-Z0-9]+)+$"
/>

That part of the string that follows the / character allows a single
instance of one of the following characters multuple times [-?|+?|.?|_?] but
the problem is I don't know how to prevent two such allowable characters to
preceed or follow one another...

// currently matched but this pattern needs to fail
type/x-.name
// currently matched but this pattern needs to fail
type/x.-name

Got any idea how to express this rule?
 
What about:

^([a-z]+-?[a-z]+)/([a-zA-Z0-9]+[-+_\.]?[a-zA-Z0-9]+)+$

I am not familliar with the rules for mime type syntax, but this should
return valid results for the examples you have provided.
 
Back
Top