如何找到给定正则表达式生成的最小,最大长度字符串?

use*_*766 7 python regex min maxlength minmax

如何在正则表达式下找到最小和最大长度?

例如

[1-9]?[0-9]
Run Code Online (Sandbox Code Playgroud)

这个正则表达式可以生成最小1(0或1 0r 2 ....或9)和最大字符串长度2(10或11或12或...... 19或20或21 .... .......或99)

同样,任何人都可以提供一个函数,可以计算正则表达式的最小和最大长度吗?哪个可以在正则表达式下输入?

^[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])$
Run Code Online (Sandbox Code Playgroud)

Alf*_*lfe 5

正则表达式只包含一小部分元素.

  1. 原子(例如a[a-k].),
  2. 选择(例如r1|r2),
  3. 重复(例如r{3,10},r+,r*,r?).
  4. (r)可以重复或选择的群体(例如).
  5. 特价商品(例如^,$).

除非我们想要添加非消费前瞻和类似内容,否则它或多或少是它,但它们不是您的示例输入的一部分,因此我不会考虑这些.

这些可以多久(最小/最大)?

  1. 1/1(原子大小不变)
  2. min(minlen(r)for r in choices)/ max(maxlen(r)for r in choices)
  3. minlen(r)*minrepretition/maxlen(r)*maxrepetition
  4. minlen(r)/ maxlen(r)
  5. 0(位置参数与空字符串匹配).

所以,你需要的是一个正则表达式解析器(正如Hugh Bothwell在他的回答中所说的那样),它会像给定正则表达式的抽象语法树(absy)那样返回你; 然后可以使用我在上面概述的规则来分析这个绝对,以找到给定正则表达式可以匹配的字符串的最小或最大长度.


Aar*_*all 2

Looks like you need to build a regex parser to parse these regular expressions and calculate that for you. Something that would look at brackets as a single character, braces as the variable len, and the |'s for more variability. Looks like you've got a lot of homework in front of you. Good luck!

Edit, some additional help.

Ok, here's a bit to perhaps get you started:

This regular expression, for example:

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

So this regular expression would be 1 to 30 characters long.

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