Skip to main content
 首页 » 编程设计

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

2024年08月20日30zdz8207

我正在尝试创建一个正则表达式来查找经纪商数据中的期权符号。每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 个空格而不是一个。您可以通过向规则添加 +(“至少一次”)来解决此问题:

(\s+)?                # optional separator