Python 股票期权的正则表达式不匹配

Python Regex for Equity Option not Matching

我正在尝试创建一个正则表达式来查找经纪商数据中的期权符号。每 Wikipedia 格式为:

  1. 标的股票或 ETF 的根代码,以空格填充至 6 个字符
  2. 到期日期,6 位数字,格式为 yymmdd
  3. 看跌或看涨期权类型,P 或 C
  4. 行权价,为价格x 1000,前面补0至8位

所以我创建了这个正则表达式:

option_regex = re.compile(r'''(
(\w{1,6})            # beginning ticker, 1 to 6 word characters
(\s)?                # optional separator
(\d{6})              # 6 digits for yymmdd
([cp])               # C or P for call or put
(\d{8})              # 8 digits for strike price
)''', re.VERBOSE | re.IGNORECASE)

但是当我测试它时出现错误:

import re

option_regex = re.compile(r'''(
(\w{1,6})            # beginning ticker, 1 to 6 word characters
(\s)?                # optional separator
(\d{6})              # 6 digits for yymmdd
([cp])               # C or P for call or put
(\d{8})              # 8 digits for strike price
)''', re.VERBOSE | re.IGNORECASE)

result = option_regex.search('AAPL  170818C00155000')

result.group()
Traceback (most recent call last):

  File "<ipython-input-4-0273c989d990>", line 1, in <module>
    result.group()

AttributeError: 'NoneType' object has no attribute 'group'

来自python documentation on re.search()

Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding MatchObject instance. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

您的代码抛出此异常,因为子例程未找到任何内容。基本上,您正在尝试在 None 上 运行 .group()。防御它是个好主意:

if not result:
    ... # Pattern didn't match the string
    return

您的模式与您输入的字符串不匹配,因为它的分隔符比您假设的要长:它有 2 个空格而不是一个。您可以通过向规则添加 + ("at-least-once") 来解决此问题:

(\s+)?                # optional separator