简体   繁体   English

匹配股票代码的正则表达式模式

[英]Regex pattern to match ticker symbol

I am trying to match ticker symbols that have the following format:我正在尝试匹配具有以下格式的股票代码:

Part1: A market identifier code (MIC) which is used to specify exchange on which the securities are traded.第 1 部分:市场标识符代码 (MIC),用于指定进行证券交易的交易所。 The code is unique, includes four characters, and starts with X, followed by a three-digit code to signify the market, such as XNAS for the Nasdaq market.代码是唯一的,包括四个字符,以 X 开头,后跟一个表示市场的三位数代码,例如纳斯达克市场的 XNAS。

Part1 is separated from Part2 by a colon. Part1 与 Part2 之间用冒号分隔。

Part2: A ticker code which has two parts: (a) The security code which is typically anything from 1 char (F for Ford), to 5 chars (VFIAX for the Vanguard 500 Index).第 2 部分:股票代码由两部分组成:(a) 安全代码,通常为 1 个字符(F 代表福特)到 5 个字符(VFIAX 代表 Vanguard 500 指数)。 Th (b) An optional part which can be further split into (i) Expiration date, 6 digits in the format yymmdd (ii) Option type, either P or C, for put or call (iii) Strike price, as the price x 1000, front padded with 0s to 8 digits Th (b) 一个可选部分,可以进一步拆分为 (i) 到期日期,6 位数字,格式为 yymmdd (ii) 期权类型,P 或 C,用于看跌或看涨 (iii) 执行价格,作为价格 x 1000,前面填充了 0 到 8 位数字

A gotcha that I need to handle is that when the optional part is present ( sometimes ) the security code is padded with spaces to 6 characters.我需要处理的一个问题是,当可选部分存在时(有时),安全代码用空格填充到 6 个字符。

So I need to match the following valid tickers:所以我需要匹配以下有效代码:

XLON:SBRY
XNAS:TSLA
XCME:SPX   141122P00019500
XNAS:AAPL200918C00032500

My regexfu is not great, and this is what I've managed to come up with so far:我的 regexfu 不是很好,这是我迄今为止设法想出的:

^(X)(AZ){3}(:)(\\d|[AZ]){1,6}\\s

What is the correct regex that matches all of the above valid ticker symbols and matches the parts correctly?与上述所有有效股票代码匹配并正确匹配部分的正确正则表达式是什么?

You could get the matches using:您可以使用以下方法获取匹配项:

^X[A-Z]{3}:[A-Z]{1,5}(?:\s*\d{6}[PC]\d{8})?$

Regex demo正则表达式演示

A bit more precise match for the month/day and the security code padded with spaces to 6 characters could be:月/日的更精确匹配以及用空格填充到 6 个字符的安全代码可能是:

^X[A-Z]{3}:[A-Z]{1,5}(?: {0,6}\d{2}(?:0[1-9]|1[012])(?:0[1-9]|[12][0-9]|3[01])[PC]\d{8})?$

Explanation解释

  • ^ Start of string ^字符串开始
  • X[AZ]{3}: Match X, 3 chars AZ and : X[AZ]{3}:匹配 X、3 个字符 AZ 和:
  • [AZ]{1,5} Match 1-5 times AZ [AZ]{1,5}匹配 1-5 次 AZ
  • (?: Non capture group (?:非捕获组
    • {0,6}\\d{2} Match 0-6 spaces and 2 digits {0,6}\\d{2}匹配 0-6 个空格和 2 个数字
    • (?:0[1-9]|1[012]) Match a month part 01 - 12 (?:0[1-9]|1[012])匹配月份部分 01 - 12
    • (?:0[1-9]|[12][0-9]|3[01]) Match a day part 01 - 31 (?:0[1-9]|[12][0-9]|3[01])匹配一天部分 01 - 31
    • [PC]\\d{8} Match P or C and 8 digits [PC]\\d{8}匹配PC和 8 位数字
  • )? Close group and make it optional关闭组并使其成为可选
  • $ End of string $字符串结尾

Regex demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM