in Education by
I need to remove zeros that are in the middle of a string, while keeping the ones at the end (in pyspark). So far I have only found regex that remove leading or trailing zeros. Example: df1 = spark.createDataFrame( [ ("GH0786",), ("HH7040",), ("IP0090",), ("AH567",), ], ["number"] ) INPUT: +-------+ |number | +-------+ |GH0786 | |HH7040 | |IP0090 | |AH567 | +-------+ EXPECTED OUTPUT: +-------+ |number | +-------+ |GH786 | |HH740 | |IP90 | |AH567 | +-------+ I have thought about splitting the string at the first zero and remove the last character (the 0) of the first substring (and if the first character of the second substring starts with a zero as well, then remove the first character), but was wondering if there was a more elegant way. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use 0+(?!$) to match zeros that are not at the end of the strings; ?! means negative look ahead, $ matches the end of the string so (?!$) matches character not at EOS: import pyspark.sql.functions as F df1.withColumn('zeroRemoved', F.regexp_replace('number', '0+(?!$)', '')).show() +------+-----------+ |number|zeroRemoved| +------+-----------+ |GH0786| GH786| |HH7040| HH740| |IP0090| IP90| | AH567| AH567| +------+-----------+

Related questions

0 votes
    I need to remove zeros that are in the middle of a string, while keeping the ones at the end (in ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I need to remove zeros that are in the middle of a string, while keeping the ones at the end (in ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    Mother, father and son line up at random for a family picture E : son on one end, F : father in middle Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    Find a 10-digit number, where the first figure defines the count of zeros in this number, the second figure the ... character expresses the count of the numeral 9 in this number....
asked Feb 13, 2021 in Education by JackTerrance
0 votes
    Write a Numpy program to create an array to 10 zeros,10 ones,10 fives. Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    I had to add some ugly links in my website that looks like: "/page/?utm_source=value&utm_source= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have a UiTextView I use to display detail text. Sometimes the text can be lengthy enough to scroll ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have some website which requires a logon and shows sensitive information. The person goes to the page, is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a big string (a base64 encoded image) and it is 1050 characters long. How can I append a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    I have a big string (a base64 encoded image) and it is 1050 characters long. How can I append a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Me and my colleague have different versions of VisualStudio. He used interpolated string and I couldn't ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Which of the following matches end of the string using regular expression in java? (a) \z (b) \\ (c ... Regular Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
0 votes
    If the number of zeros (n) are greater than the number of poles (m), then there will be _________ number of ... for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 16, 2021 in Education by JackTerrance
0 votes
    If the number of poles (m) are greater than the number of zeros (n), then there will be _________ number of ... for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 16, 2021 in Education by JackTerrance
...