简体   繁体   English

Python 正则表达式无法找到模式 - 在 Apache Spark 上使用 pyspark

[英]Python regular expression unable to find pattern - using pyspark on Apache Spark

Can someone let me why the regular expression有人可以让我为什么正则表达式

df = df2.withColumn("extracted", F.regexp_extract("title", "[Pp]ython", 0))

Can find the pattern 'Python' or 'python' from the followng column called title可以从名为 title 的后续列中找到模式“Python”或“python”

title
A fast PostgreSQL client library for Python: 3x faster than psycopg2
A project template for data science in Python
A simple python framework to build/train LUIS models
An Introduction to Stock Market Data Analysis with Python (Part 1)
Asynchronous Python
Cubr  A Rubiks Cube Solver Written in Python and using Webcam Input (2013)
Python 4 Kids: Python for Kids: Python 3  Project 10

But the regular expression can't find the pattern Python or python from the following但是正则表达式在下面找不到模式 Python 或 python

title
Python Core Development Sprint 2016: 3.6 and beyond
Hypothesis.works articles: 3.5.0 and 3.5.1 Releases of Hypothesis for Python
Total pip packages downloaded, separated by Python versions (June  August 2016)
PEP 530: Asynchronous Comprehensions in Python 3.6
Python 2.7 still reigns supreme in pip installs
CheckiO  games for Python and JavaScript coders. ClassRoom support is included
VR Zero, Virtual Reality on the RaspberryPi, in Python

Thanks谢谢

Use the ignore case regex;使用忽略大小写正则表达式;

(?i) -ignore or case-insensitive mode ON (?i) -ignore or case-insensitive mode ON

Data数据

data=[数据=[

  (1,"Python Core Development Sprint 2016: 3.6 and beyond"),
  (2,"Hypothesis.works articles: 3.5.0 and 3.5.1 Releases of Hypothesis for Python"),
  (3,"CheckiO  games for python and JavaScript coders. ClassRoom support is included")
  ]
df=spark.createDataFrame(data, ['id','title'])
df.show(truncate=False)

Solution解决方案

df.withColumn('extract', F.regexp_extract(col('title'),'(?i)[P]ython',0)).show()

Outcome结果

+---+--------------------+-------+
| id|               title|extract|
+---+--------------------+-------+
|  1|Python Core Devel...| Python|
|  2|Hypothesis.works ...| Python|
|  3|CheckiO  games fo...| python|
+---+--------------------+-------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM