Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 具有多个潜在模式的正则表达式_Regex_Pandas - Fatal编程技术网

Regex 具有多个潜在模式的正则表达式

Regex 具有多个潜在模式的正则表达式,regex,pandas,Regex,Pandas,我有: 我想从每一行中提取股票代码 blah blah blah (3383-HK) - blah blah blah blah blah blah (A-US) - blah blah blah blah blah blah (blah) (3383-HK) - blah blah blah blah blah blah (A.CC-US) - blah blah blah 股票代码格式为: 括号内 格式为XXX-XXX或XXX.XXX-XXX,其中XXX为字母数字字符,每个部分的长度范围

我有:

我想从每一行中提取股票代码

blah blah blah (3383-HK) - blah blah blah
blah blah blah (A-US) - blah blah blah
blah blah blah (blah) (3383-HK) - blah blah blah
blah blah blah (A.CC-US) - blah blah blah
股票代码格式为:

  • 括号内
  • 格式为XXX-XXX或XXX.XXX-XXX,其中XXX为字母数字字符,每个部分的长度范围为1到20个字符
目前,我使用:

(3383-HK
(A-US
(3383-HK
(A.CC-US
它返回:

df['ticker_region']=df['filename'].str.extract(r"(\(\w{1,20}-\w{1,20})")
但不是: (A.CC-US)

有没有推荐的方法来提取最后一个

我试过:

(3383-HK
(3383-HK
(A.CC-US
但它不起作用,因为它不能抓住:

df['ticker_region']=df['filename'].str.extract(r"(\(\w{1,20}\.?\w{1,10}?-\w{1,20})")
我的完整脚本是:

(A-US
输出

df.A.str.extract(r'\(([\w\.?]+-.*)\)')
你可以用

          0
0   3383-HK
1   A-US
2   3383-HK
3   A.CC-US

详细信息

  • \(
    -a
    字符
  • (((?=[\w.]{1,20}-)\w+(?:\.\w+)-\w{1,20})
    -组1(由
    Series.str.extract
    返回):
    • (?=[\w.]{1,20}-
      -必须有1到20个单词或
      字符,然后紧靠当前位置右侧
      -
    • \w+
      -1+字字符
    • (?:\。\w+)
      可选的
      和1+字字符序列
    • -
      -a
      -
      字符
    • \w{1,20}
      -1到20个字字符
  • \)
    -a
    字符

描述太多,但您没有解释要求。您的意思是代码必须遵守一些规则,比如1)以
,2)然后必须有1到20个单词字符,中间必须有一个可选点,3)然后
-
,4)然后是任何1到20个字母、数字、
?或者你真的只是在括号之间提取文本?问题已经更新,显示了提取要求。我发布了一个假设,即第一个“单词”中的
计入长度限制。
          0
0   3383-HK
1   A-US
2   3383-HK
3   A.CC-US
\(((?=[\w.]{1,20}-)\w+(?:\.\w+)?-\w{1,20})\)