使用python或pyspark中的regex从字符串中提取所需的字符间数据



我想从数据帧列的行中的字符串数据中提取几个字符之间的数据。例如,我在下面的列中有数据:

+----------------------------------------------------+
|                                               Azure|
+----------------------------------------------------+
|{ref=[As Tailwind Traders gets, started with Azure]}|
|{ref=first steps}                                   |
|{ref=will be to create}                             |
|{ref=at least one Azure subscription}               |
+----------------------------------------------------+

并希望以这种方式进行转换

+----------------------------------------------------+
|                                               Azure|
+----------------------------------------------------+
|As Tailwind Traders gets, started with Azure        |
|first steps                                         |
|will be to create                                   |
|at least one Azure subscription                     |
+----------------------------------------------------+

所以我应该提取";[]";以及带有单个元素的行,并使用pyspark/python-regex将其放回同一列或新列中要删除的东西-"ref=",外部"{}">

注意-我尝试使用regex_replace函数,但它也替换了所需数据中的[],{}

那么,如何在pyspark中使用regex来实现这一点呢?

您可以使用以下模式,将\1放入替换字符串中。

"{ref=[?([,ws]+)]?}"gm

请参阅https://regex101.com/r/OyFBkJ/1

最新更新