Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, you could make use of the fact that Python's regex engine only allows fixed-length regular expressions in lookbehind assertions:</p> <pre><code>import re regexes = [r".x{2}(abc|def)", # fixed r"a|bc", # variable/finite r"(.)\1", # fixed r".{0,3}", # variable/finite r".*"] # variable/infinite for regex in regexes: try: r = re.compile("(?&lt;=" + regex + ")") except: print("Not fixed length: {}".format(regex)) else: print("Fixed length: {}".format(regex)) </code></pre> <p>will output</p> <pre><code>Fixed length: .x{2}(abc|def) Not fixed length: a|bc Fixed length: (.)\1 Not fixed length: .{0,3} Not fixed length: .* </code></pre> <p>I'm assuming that the regex itself is valid.</p> <p>Now, how does Python know whether the regex is fixed-length or not? Just read the source - in <code>sre_parse.py</code>, there is a method called <code>getwidth()</code> that returns a tuple consisting of the lowest and the highest possible length, and if these are not equal in a lookbehind assertion, <code>re.compile()</code> will raise an error. The <code>getwidth()</code> method walks through the regex recursively:</p> <pre><code>def getwidth(self): # determine the width (min, max) for this subpattern if self.width: return self.width lo = hi = 0 UNITCODES = (ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY) REPEATCODES = (MIN_REPEAT, MAX_REPEAT) for op, av in self.data: if op is BRANCH: i = sys.maxsize j = 0 for av in av[1]: l, h = av.getwidth() i = min(i, l) j = max(j, h) lo = lo + i hi = hi + j elif op is CALL: i, j = av.getwidth() lo = lo + i hi = hi + j elif op is SUBPATTERN: i, j = av[1].getwidth() lo = lo + i hi = hi + j elif op in REPEATCODES: i, j = av[2].getwidth() lo = lo + int(i) * av[0] hi = hi + int(j) * av[1] elif op in UNITCODES: lo = lo + 1 hi = hi + 1 elif op == SUCCESS: break self.width = int(min(lo, sys.maxsize)), int(min(hi, sys.maxsize)) return self.width </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload