1

I have a string AF1234 and I want to print 1234 , also sometimes I get only numbers like 25 and 23 and I want these numbers as well, hence I am trying to use a regex_replace to check the string, below is the syntax:

select substring(nvl(nullif(regexp_replace(regexp_replace('AF1234','A$',''),'[[:digit:]]',''),''),'XX'),1,2)

This works in impala but not hive

1
  • add sample input and output to get more clarity. Commented Jul 2, 2020 at 11:25

2 Answers 2

1

Tested in Hive, Impala, Spark SQL

select regexp_extract('AF1234','(\\d+)',1)

Result:

1234
Sign up to request clarification or add additional context in comments.

Comments

0

If you want numbers out of alphanumeric try this,

scala> spark.sql("select regexp_replace('AF1234','[A-Z]*','')").show
+------------------------------+
|regexp_replace(AF1234, [A-Z]*, )|
+------------------------------+
|                          1234|
+------------------------------+

spark.sql("select regexp_replace('23','[A-Z]*','')").show
+----------------------------+
|regexp_replace(23, [A-Z]*, )|
+----------------------------+
|                          23|
+----------------------------+

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.