- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ11-23-2022 09:41 AM
hello everyone, I'm creating a regex expression to fetch only the value of a string, but some values โโare negative. I am not able to create the rule to compose the negative value. can you help me?
from pyspark.sql.functions import regexp_extract
from pyspark.sql.types import StructType,StructField, StringType
data = [("01","[$R$-pt-BR] 150.00"),
("02", "-[$R$-pt-BR] 379.52" ),
("03", "[$R$-pt-BR] 185.16" ),
("04", "[$R$-pt-BR] 185.16" ),]
schema = StructType([ \
StructField("id",StringType(),True), \
StructField("description",StringType(),True),
])
df = spark.createDataFrame(data=data,schema=schema)
df.display()
df1 = df\
.withColumn("value", regexp_extract('description', r"[\d]{1,4}.[\d]{1,4}", 0))
df1.display()
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ11-24-2022 12:59 AM
Hi there,
- Create a column to catch the minus "-": pattern is: "^[\-]?"
- Create a column to catch the digits that you already done.
- Concat these two columns above.
Hope it fit your requirement. ๐
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ11-23-2022 10:50 AM
@Werner Stinckensโ
can you help me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ11-24-2022 12:59 AM
Hi there,
- Create a column to catch the minus "-": pattern is: "^[\-]?"
- Create a column to catch the digits that you already done.
- Concat these two columns above.
Hope it fit your requirement. ๐
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ11-24-2022 05:46 AM
df1 = df\
.withColumn("value", regexp_extract('description', "[\d]{1,4}.[\d]{1,4}", 0))\
.withColumn("operador", regexp_extract('description', "^[\-]?", 0))\
.withColumn("value2", concat("operador","value"))
df1.display()
@Nhat Hoangโ , Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ11-24-2022 04:26 AM
I found another solution, but I didn't want to give up on regex. If you find a way, be sure to post. Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ11-24-2022 07:36 AM
\s*\[[^)]*\] removes the square brackets and everything inside it and the space too (well actually substitutes it with nothing).
https://regex101.com/r/tv9pbJ/1
Haven't checked if spark can do regex substitution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ12-01-2022 03:25 AM
its like you need to find this pattern : "^[\-]?"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ03-16-2023 05:50 AM
Have you found the answer? If you are a student in college or school searching for free essay examples online, you may want to visit the website https://writinguniverse.com/free-essay-examples/soccer/ here you will find a vast collection of free essay examples related to various topics, including soccer. These essay examples can be valuable resources to help you complete your essay assignments.