Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Limit total length of an expression

Last post 11-18-2009, 4:56 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  11-18-2009, 2:31 AM 57382

    Limit total length of an expression

    Hi to All,

    Which is the way/best practice limiting _total_ length of an expression. I am validating data input with regex, phone email etc. For example given the following email regex:([A-Za-z0-9_%+-]\.?)+@([A-Za-z0-9-]+\.)+([A-Za-z0-9]{2,4}). I would like to limit to total length to 100 characters. 

    thx for answers 

     

     

  •  11-18-2009, 8:51 AM 57389 in reply to 57382

    Re: Limit total length of an expression

    probably be quicker and easier to use your language construct for the length of the string like php's strlen or c# str.length test for <= 100 then run your regex otherwise you would have a rather bloated regex
  •  11-18-2009, 4:56 PM 57401 in reply to 57382

    Re: Limit total length of an expression

    You don't say what regex variant you are using so this may not work, but you could try prefixing the pattern with something like:

    (?=^.{1,100}$)

    This assumes that you want to limit the whole pattern to 100 (or whatever number you put in) characters, that you are counting characters (not treating "==" (say) as a single item) and that you are counting all characters.

    This will check (but not move the match starting point or capture any text) that there is at least 1 character and at most 100 characters between the start and the end. If not then the pattern will fail. Otherwise, the rest of the pattern is tried as usual.

    Susan

View as RSS news feed in XML