【问题标题】:Finding a regx expression in pyspark?在 pyspark 中找到正则表达式?
【发布时间】:2019-01-10 12:13:16
【问题描述】:
I have a column in pyspark dataframe which contain values separated by ; 

+----------------------------------------------------------------------------------+
|name                                                                              |
+----------------------------------------------------------------------------------+
|tppid=dfc36cc18bba07ae2419a1501534aec6fdcc22e0dcefed4f58c48b0169f203f6;xmaslist=no|
+----------------------------------------------------------------------------------+

因此,如果我使用此列,则可以在此列中出现任意数量的键值对

df.withColumn('test', regexp_extract(col('name'), '(?<=tppid=)(.*?);', 1)).show(1,False)

我可以提取 tppid,但是当 tppid 作为一行中的最后一个键值对出现时,它无法提取,我想要一个 regx,它可以提取一个键的值,只要它在一行中的位置。

【问题讨论】:

    标签: python regex pyspark pyspark-sql


    【解决方案1】:

    您可以使用否定字符类 [^;] 来匹配除 ; 之外的任何字符:

    tppid=([^;]+)
    

    regex demo

    由于regexp_extract 的第三个参数是1(访问第1 组内容),您可以放弃lookbehind 构造并使用tppid= 作为消费模式的一部分。

    【讨论】:

      【解决方案2】:

      除了 Wiktor Stribiżew 的回答之外,您还可以使用锚点。 $ 表示字符串的结尾。

      tppid=\w+(?=;|\s|$) 
      

      还有this 正则表达式只为您提取没有tppid= 部分的值:

      (?<=tppid=)\w+(?=;|\s|$)
      

      【讨论】:

      • 但是,这些模式不适用于 OP 当前代码。
      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2020-09-21
      相关资源
      最近更新 更多