Regex to restrict length in ocamllex

I want to restrict the length of matched substring in regular expression. I want to use this regex in ocamllex rule. Below is the regex that I am trying but it is giving me syntax error:

([^ '(' ')' '\\' ' ' '\t' '\r' 'n']{1,10}$)

Is this not supported in ocamllex?

This is not supported – see the Section in the OCaml Manual. You can’t match the end of input using $ but it can be matched using eof.

Depending on what you need there are several options.

  • Create a pattern explicitly that matches what you need.
  • Create a rule that matches one character at the time and counts characters
  • Match a possibly longer string but check its length afterwards

This seems like an odd thing to do in a lexer.
What are you trying to achieve? Maybe there is a better way.

Okay, Thank you lindig for your suggestions.