Jump to content
UBot Underground

Regex - Is There An Easier Way To Do This


Recommended Posts

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,

Link to post
Share on other sites

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")

  • Like 1
Link to post
Share on other sites

 

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

Link to post
Share on other sites

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

Link to post
Share on other sites

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)
  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...