Yagami 1 Posted November 21, 2017 Report Share Posted November 21, 2017 Hi. I'm trying to extract "xx.xk" in the string "17 board • 2.1k followers" and format it to 2100. Here is my code: set(#string,$scrape attribute(<class="_st _ss _sw _sk _5j _sm _sq _no _np _nq _nr">,"innertext"),"Global") set(#k,$find regular expression(#string,"[0-9]*.[0-9]k"),"Global") set(#k,$replace(#k,"k",00),"Global") set(#k,$replace(#k,".",""),"Global") Is there a better way to do this? Thank you, Quote Link to post Share on other sites
Sanjeev 46 Posted November 21, 2017 Report Share Posted November 21, 2017 A slightly better way... I am trying to account for any number of followers.. 0,10,200,245 ,2.5k etc set(#scraped string,"17 board • 2.1k followers","Global") set(#result,$replace($replace($find regular expression(#scraped string,"\\d+(\\.\\d+k)?(?= followers)"),".",","),"k",00),"Global") I am also assuming that there is a single space after 'followers'. Code can be made more robust by allowing zero or more spaces after followers. set(#scraped string,"17 board •22.7k followers","Global") set(#result,$replace($replace($find regular expression(#scraped string,"\\d+(\\.\\d+k)?(?=\\s*followers)"),".",","),"k",00),"Global") 1 Quote Link to post Share on other sites
Varo 28 Posted November 22, 2017 Report Share Posted November 22, 2017 A slightly better way... I am trying to account for any number of followers.. 0,10,200,245 ,2.5k etc set(#scraped string,"17 board • 2.1k followers","Global") set(#result,$replace($replace($find regular expression(#scraped string,"\\d+(\\.\\d+k)?(?= followers)"),".",","),"k",00),"Global") I am also assuming that there is a single space after 'followers'. Code can be made more robust by allowing zero or more spaces after followers. set(#scraped string,"17 board •22.7k followers","Global") set(#result,$replace($replace($find regular expression(#scraped string,"\\d+(\\.\\d+k)?(?=\\s*followers)"),".",","),"k",00),"Global") Thanks Sanjeev, i learn some lesson in your regex Quote Link to post Share on other sites
Yagami 1 Posted November 22, 2017 Author Report Share Posted November 22, 2017 Hi, thank you for taking from your time to help.The code works, but there is one exception that I didn't think of and it's when we have 1k followers for example. The code above will return 1,00. Is there a quick work around with Regex or I have to use conditions? Thanks again Quote Link to post Share on other sites
Varo 28 Posted November 22, 2017 Report Share Posted November 22, 2017 Try these code set(#scraped string,"17 board • 2.1k followers 17 board • 521 followers 17 board • 1k followers 17 board • 781 followers","Global") set(#result,$replace regular expression($replace regular expression($replace($find regular expression(#scraped string,"\\d+(\\.)?(\\d+)?k?(?=\\s*followers)"),".",","),"(?<=,\\d)k",00),"k",",000"),"Global") alert(#result) 1 Quote Link to post Share on other sites
Yagami 1 Posted November 22, 2017 Author Report Share Posted November 22, 2017 Great, thank you. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.