polars.Series.str.extract_groups#
- Series.str.extract_groups(pattern: str) Series[source]#
提取给定正则表达式模式的所有捕获组。
- Parameters:
- pattern
一个有效的正则表达式模式,包含至少一个捕获组,与regex crate兼容。
- Returns:
- Series
数据类型为
Struct的系列,其字段的数据类型为String。
注释
所有组名都是字符串。
如果你的模式包含未命名的组,它们的数字位置会被转换为字符串。
例如,我们可以通过字符串
"1"访问第一个组:>>> ( ... pl.Series(["foo bar baz"]) ... .str.extract_groups(r"(\w+) (.+) (\w+)") ... .struct["1"] ... ) shape: (1,) Series: '1' [str] [ "foo" ]
示例
>>> s = pl.Series( ... name="url", ... values=[ ... "http://vote.com/ballon_dor?candidate=messi&ref=python", ... "http://vote.com/ballon_dor?candidate=weghorst&ref=polars", ... "http://vote.com/ballon_dor?error=404&ref=rust", ... ], ... ) >>> s.str.extract_groups(r"candidate=(?<candidate>\w+)&ref=(?<ref>\w+)") shape: (3,) Series: 'url' [struct[2]] [ {"messi","python"} {"weghorst","polars"} {null,null} ]