如何使用正则表达式替换替换特殊字符?
how to use regex replace to replace special character?
我正在尝试使用正则表达式替换将“\”替换为 \ 但没有得到正确的解决方案。想要删除即将出现的双引号。你能帮我看看怎么做吗?
示例:
"\""warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"
结果:
\"warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"
这是否解决了您的问题?
re.sub(r'"\"', r'\', text)
尝试以下解决方案:
df = spark.createDataFrame([
(1, '"\""warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"')
], ("ID","textVal"))
import pandas as pd
from pyspark.sql.functions import regexp_replace, col
pd.set_option('max_colwidth', 200)
df2 = df.withColumn('textVal', regexp_replace(col('textVal'), '\"\\\"', '\\'))
df2.toPandas()
ID textVal
0 1 \"warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"
希望对您有所帮助!
我正在尝试使用正则表达式替换将“\”替换为 \ 但没有得到正确的解决方案。想要删除即将出现的双引号。你能帮我看看怎么做吗?
示例:
"\""warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"
结果:
\"warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"
这是否解决了您的问题?
re.sub(r'"\"', r'\', text)
尝试以下解决方案:
df = spark.createDataFrame([
(1, '"\""warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"')
], ("ID","textVal"))
import pandas as pd
from pyspark.sql.functions import regexp_replace, col
pd.set_option('max_colwidth', 200)
df2 = df.withColumn('textVal', regexp_replace(col('textVal'), '\"\\\"', '\\'))
df2.toPandas()
ID textVal
0 1 \"warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"
希望对您有所帮助!