當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python pyspark Series.str.findall用法及代碼示例

本文簡要介紹 pyspark.pandas.Series.str.findall 的用法。

用法:

str.findall(pat: str, flags: int = 0) → ps.Series

查找係列中所有出現的模式或正則表達式。

相當於將re.findall() 應用於係列中的所有元素。

參數

patstr

模式或正則表達式。

flagsint,默認 0(無標誌)

re 模塊標誌,例如re.IGNORECASE

返回

係列對象

此係列的每個字符串中的模式或正則表達式的所有非重疊匹配。

例子

>>> s = ps.Series(['Lion', 'Monkey', 'Rabbit'])

搜索模式“Monkey”會返回一個匹配項:

>>> s.str.findall('Monkey')
0          []
1    [Monkey]
2          []
dtype: object

另一方麵,模式“MONKEY”的搜索不返回任何匹配:

>>> s.str.findall('MONKEY')
0    []
1    []
2    []
dtype: object

可以將標誌添加到模式或正則表達式中。例如,要找到忽略大小寫的模式“MONKEY”:

>>> import re
>>> s.str.findall('MONKEY', flags=re.IGNORECASE)
0          []
1    [Monkey]
2          []
dtype: object

當模式匹配 Series 中的多個字符串時,返回所有匹配項:

>>> s.str.findall('on')
0    [on]
1    [on]
2      []
dtype: object

也支持正則表達式。例如,搜索以單詞‘on’ 結尾的所有字符串如下所示:

>>> s.str.findall('on$')
0    [on]
1      []
2      []
dtype: object

如果在同一個字符串中多次找到該模式,則返回多個字符串的列表:

>>> s.str.findall('b')
0        []
1        []
2    [b, b]
dtype: object

相關用法


注:本文由純淨天空篩選整理自spark.apache.org大神的英文原創作品 pyspark.pandas.Series.str.findall。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。