0

I am trying to replaces a regex (in this case a space with a number) with

I have a Spark dataframe that contains a string column. I want to replace a regex (space plus a number) with a comma without losing the number. I have tried both of these with no luck:

df.select("A", f.regexp_replace(f.col("A"), "\s+[0-9]", ' , ').alias("replaced"))

df.select("A", f.regexp_replace(f.col("A"), "\s+[0-9]", '\s+[0-9] , ').alias("replaced"))

Any help is appreciated.

3

1 Answer 1

0

What you need is another function, regex_extract

So, you have to divide the regex and get the part you need. It could be something like this:

df.select("A", f.regexp_extract(f.col("A"), "(\s+)([0-9])", 2).alias("replaced"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.