【问题标题】:PySpark regular expression pattern matchingPySpark 正则表达式模式匹配
【发布时间】:2020-01-22 10:54:55
【问题描述】:

我想将下面的正则表达式转换为 pyspark。

例如:+420602642919- 我们需要使用正则表达式过滤这类电话号码

("/^((\+|00)(42)|0)?((060|660|664|676|68[0-1]|688)([0-9]){7}|(699)([0-9]){8})$/")

如何在 Pyspark 中实现这一点?

【问题讨论】:

  • 您能提供一些示例数据吗?通常,您应该能够立即将上述正则表达式与 Spark 正则表达式函数之一一起使用

标签: apache-spark pyspark apache-spark-sql


【解决方案1】:

您可以使用rlike 列函数应用您的正则表达式,如下所示:

import pyspark.sql.functions as F

#your initial dataframe
l = [('+420602642919',)
,('blabla',)]

df=spark.createDataFrame(l, ['someText'])

yourregex = '^((\+|00)(42)|0)?((060|660|664|676|68[0-1]|688)([0-9]){7}|(699)([0-9]){8})$'

#adding a column
df.withColumn('match', df.someText.rlike(yourregex)).show()

#or filtering
df.filter(df.someText.rlike(yourregex)).show()

输出:

+-------------+-----+
|     someText|match|
+-------------+-----+
|+420602642919| true|
|       blabla|false|
+-------------+-----+

+-------------+
|     someText|
+-------------+
|+420602642919|
+-------------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2016-04-20
    • 2014-09-15
    相关资源
    最近更新 更多