Python pandas正则表达式匹配两组空格之间的所有项

Python pandas正则表达式匹配两组空格之间的所有项,python,python-3.x,pandas,Python,Python 3.x,Pandas,如何将所有内容匹配到冒号左边正好3个空格和右边正好3个空格之间的特殊字符?示例用W表示为空白 示例匹配: \\s\\s\\sdata\\sstuff:\\s\\sfound\\ssome([%$)Data\\sas\\swhiteSpace\\s\\s\\s data stuff: found some([%$)Data as whiteSpace 示例不匹配: \\s\\sdata\sstuff:\\s\\sfound\\sno\\sdatacause\\sno\\s3\\ss

如何将所有内容匹配到冒号左边正好3个空格和右边正好3个空格之间的特殊字符?示例用W表示为空白

示例匹配:

\\s\\s\\sdata\\sstuff:\\s\\sfound\\ssome([%$)Data\\sas\\swhiteSpace\\s\\s\\s
   data stuff:  found some([%$)Data as whiteSpace   
示例不匹配:

\\s\\sdata\sstuff:\\s\\sfound\\sno\\sdatacause\\sno\\s3\\sspaces\\sbefore\\sor\\safter\\s\\s
  data stuff:  found no datacause no 3 spaces before or after   
其目的是将其扩展为将列从数据帧的单个列中分离出来

预期产出:

data stuff                                data stuff 2
found some([%$)Data as whiteSpace         if i had more examples for data stuff 2 it would show here
extra random data to add into a outputdf  if i had more examples for data stuff 2 it would show here
最初的想法是使用这样的东西,但这不太管用

"(\\s\\s\\s(.*?)\\:\\s\\s(.*?)\\s\\s\\s)"
把它拆了

  • (?:^ |[^])
    -匹配不是空格或是行首的任何内容
  • x(.*x)
    匹配任意一侧3个空格之间的任何内容(添加x以避免空格消失)
  • (?:$|[^])
    匹配不是空格或在行尾的任何内容

考虑一下这个df

    col
0   data stuff:   found   some([%$)Data as whiteSpace   1
Regex1:

df.col.str.extract(':\\s{3}(.*)\s{3}')
会回来吗

0    found   some([%$)Data as whiteSpace
Name: col, dtype: object
即查找之前的三个空格与1之前结尾处的三个空格之间的内容

何处为

df.col.str.extract(':\\s{3}(.*?)\s{3}') #note the ? after .*
会回来的

0    found
Name: col, dtype: object
这是三个空格的第一个和第二个实例之间的内容


如果您提供更多的测试用例,那么您还需要正则表达式做什么就变得很清楚了。

您能澄清您想要匹配哪些字符吗?我需要匹配正好在3个空格之间的任何可能的字符
0    found
Name: col, dtype: object