使用 python 或 pyspark 中的正则表达式从字符串中的字符之间提取所需数据
Extract required data between characters from a string using regex in python or pyspark
我想从数据框列的行中存在的字符串数据中提取几个字符之间的数据。
例如,我在下面的列中有数据:
+----------------------------------------------------+
| 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 正则表达式将其放回同一列或新列
要删除的东西 - 'ref=',外部“{}”
注意 - 我尝试使用 regex_replace 函数,但它也替换了所需数据中的 [],{}
那么如何在 pyspark 中使用正则表达式来实现这一点?
您可以使用以下模式,将 \1 放入替换字符串中。
"{ref=\[?([,\w\s]+)\]?\}"gm
我想从数据框列的行中存在的字符串数据中提取几个字符之间的数据。 例如,我在下面的列中有数据:
+----------------------------------------------------+
| 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 正则表达式将其放回同一列或新列 要删除的东西 - 'ref=',外部“{}”
注意 - 我尝试使用 regex_replace 函数,但它也替换了所需数据中的 [],{}
那么如何在 pyspark 中使用正则表达式来实现这一点?
您可以使用以下模式,将 \1 放入替换字符串中。
"{ref=\[?([,\w\s]+)\]?\}"gm