How to find the minimum lines of maximum length generated by regular expression?

How to find the minimum and maximum length with regular expression?

for instance

[1-9]?[0-9] 

This regular expression can generate a minimum of 1 (0 or 1 0r 2 .... or 9) and a maximum length of line 2 (10 or 11 or 12 or ... 19 or 20 or 21 .......... . or 99)

Similarly, can someone provide a function that can calculate the minimum and maximum length with a regular expression? What can take below regex as input?

 ^[a-zA-Z0-9][a-zA-Z0-9.-]{0,64}[a-zA-Z0-9]$ ^[a-zA-Z0-9._-]{1,255}$ ^[a-zA-Z0-9 !#$'()*+,./:; =?@ \\^_`~-]{1,30}$ ^[]a-zA-Z0-9 !#$'()*+,./:; =?@ [^_`{|}~-]{0,50}$ ^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$ 
+6
source share
3 answers

Regular expressions consist of a very small set of elements.

  • (for example, a or [ak] or . ),
  • (for example, r1|r2 ),
  • repetitions (for example, r{3,10} , r+ , r* , r? ).
  • (e.g., (r) ) that may be subjected to repetition or election.
  • Special offers (e.g. ^ , $ ).

This is more or less if we do not want to add non-consumer expectations and the like, but they are not part of your input example, so I will not consider them.

How long (minimum / maximum) can be?

  • 1/1 (atoms are constant in size)
  • min (minlen (r) to select r) / max (maxlen (r) for r in options)
  • minlen (r) * minrepretition / maxlen (r) * maxrepetition
  • minlen (r) / maxlen (r)
  • 0 (positional parameters correspond to an empty string).

So, you will need a regular expression parser (as Hugh Botwell said in his answer), which returns you as an abstract syntax tree (absy) of the given regular expression; this paragraph can then be analyzed using the rules that I sketched above to find the minimum or maximum length for a string that this regular expression can match.

+5
source

There is some initial code at http://pyparsing.wikispaces.com/file/view/invRegex.py for the regex parser in pyparsing; It should not be difficult to change to do what you want.

Some tutorials can be found at http://pyparsing.wikispaces.com/Examples

+4
source

It looks like you need to put together a regular expression parser to parse these regular expressions and calculate this for you. Something that looks at the brackets as one character appears as the variable len, and | for more variability. It looks like you have a lot of homework ahead. Good luck

Change, extra help.

Well, here is a little to possibly start:

This is a regular expression, for example:

 ^[a-zA-Z0-9 !#$'()*+,./:; =?@ \\^_`~-]{1,30}$ ^^--------one of these characters--^^----^^-end of string ^---start of string ^one to thirty times 

Thus, this regular expression will be 1 to 30 characters long.

Does it help? But seriously, I'm not going to do more than that, you need to read re docs: http://docs.python.org/library/re.html

+2
source

All Articles