Python Regex ignore specific string to find next example

前端 未结 1 954
予麋鹿
予麋鹿 2021-01-29 05:22

I have the following code that runs through and strips the data in the current column and creates a secondary column with just the code in parentheses and this works wonderfully

相关标签:
1条回答
  • 2021-01-29 05:30

    You may use

    df['Folder Path'].str.extract(r'\((?!\d{4}-\d{2}\)|Data Only\))([^()]*)\)',expand=True)
    

    The regex matches

    • \( - an open parenthesis
    • (?!\d{4}-\d{2}\)|Data Only\)) - a negative lookahead that fails the match if there is

      • \d{4}-\d{2}\) - 4 digits, hyphen, 2 hyphens, )
      • | - or
      • Data Only\) - Data Only) substrinbg
    • ([^()]*) - Group 1: any 0 or more chars other than open/close parentheses

    • \) - a close parenthesis

    See the regex demo.

    0 讨论(0)
提交回复
热议问题