與正則表達式有關:sre_yield

jopen 9年前發布 | 13K 次閱讀 sre_yield

sre_yield 是用于生成正則表達式匹配結果的 Python 模塊,并盡可能的匹配到所有有效值。它采用了解析正則表達式的方式,所以你可以得到一個更加精確的結果,而不僅僅只是分散的字符串。

sre_yield 通常都無法處理反向引用、lookarounds 正則表達式,除此之外,還有在這幾種情況下也無法處理;

  • The maximum value for repeats is system-dependant -- CPython'ssremodule there's a special value which is treated as infinite (either 2**16-1 or 2**32-1 depending on build).  In sre_yield, this is taken as a literal, rather than infinite, thus (on a 2**16-1 platform):

  • Theremodule docs say "Regular expression pattern strings may not contain null bytes" yet this appears to work fine.

  • Order does not depend on greediness.

  • The regex is treated as fullmatch.

  • sre_yieldis confused by even the simplest of anchors:

代碼示例:

>>> import random
>>> v = sre_yield.AllStrings('[abc]{1,4}')
>>> len(v)
120

# Now random.choice(v) has a 3/120 chance of choosing a single letter.
>>> random.seed(1)
>>> sum([1 if len(random.choice(v)) == 1 else 0 for _ in range(120)])
3

# xeger(v) has ~25% chance of choosing a single letter, because the length
and match are chosen independently.
> from rstr import xeger
> sum([1 if len(xeger('[abc]{1,4}')) == 1 else 0 for _ in range(120)])
26

項目主頁:http://www.baiduhome.net/lib/view/home/1429276344236

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!