โ12-16-2022 04:40 PM
How to parse, extract or form a string based on a pattern?
SQL server has a function which will format the string based on a pattern. example,
a string is "abcdefgh", the pattern is XX-XX-XXXX,
the the string will be "ab-cd-efgh".
How to archive this with a function?
โ12-18-2022 07:03 PM
@Tim zhangโ ,
thanks for your code, and here is your answer
I asked this question in Stackoverflow and got this answer
Here is the Stackoverflow link- https://stackoverflow.com/questions/74845760/how-to-parse-a-pattern-and-use-it-to-format-a-string-us...
Please select my this answer as the best answer,it will be really encouraging
Thanks
Aviral Bhardwaj
โ12-17-2022 10:22 PM
โ12-18-2022 02:39 PM
Thanks a lot of your replay @Aviral Bhardwajโ
I try to not hard code it and eventually turn it to a function. This is a initial raw code, but it has problem,
val pattern="XX-***-XXXX-***";
val item="abcdefghijkl"
val patternindex=pattern.lastIndexOf("-");
val patternupdated= pattern.substring(0,patternindex);
var lenplus = pattern.indexOf("-")
var fitem=item.patch(lenplus, "-", 0)
val result = patternupdated.split("-")
for ( a
{
val len= a.length()
// Displays output
lenplus = len + lenplus+1
fitem= fitem.patch(lenplus, "-", 0)
println(fitem)
}
The result is wrong,
ab-cd-efghijkl
ab-cd-efg-hijkl
ab-cd-efg-hijk-l
โ12-18-2022 07:03 PM
@Tim zhangโ ,
thanks for your code, and here is your answer
I asked this question in Stackoverflow and got this answer
Here is the Stackoverflow link- https://stackoverflow.com/questions/74845760/how-to-parse-a-pattern-and-use-it-to-format-a-string-us...
Please select my this answer as the best answer,it will be really encouraging
Thanks
Aviral Bhardwaj
โ12-19-2022 05:47 PM
did you get any time to cehck above response @Tim zhangโ
โ12-19-2022 07:26 PM
yea, it works! Thanks!
โ12-19-2022 08:10 PM
@Tim zhangโ can you please upvote my answer and make it as best answer,it will be great help
Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโt want to miss the chance to attend and share knowledge.
If there isnโt a group near you, start one and help create a community that brings people together.
Request a New Group