How to Approach Utopian.io Regex Task Request
So Utopian.io recently created a Task Request for Developers where they wanted skilled JS Developers and Regex experts which you can check here https://steemit.com/utopian-io/@utopian.tasks/js-developers-and-regex-experts-wanted-javascript-coding-task. Now there are quite a few checks you have to do before giving the result back.
In the task request, it is specifically mentioned that that "Parse the text and compare it with a set of regular expressions", thus instead of easy split functions we should use REGEX. So lets find out how to approach this Task Request.
First Of All, in the task request it is clearly mentioned that always it starts with "@utopian-bot !utopian", that means we have some starting point. Now using the Regex Expression we will check if the input test starts with @utopian-bot !utopian or not.
I use https://regex101.com/ to check all my regex because it gives a clear explanation how my Regex Expression worked. Now since I know that my input will start with @utopian-bot !utopian I will start my regex with @utopian-bot !utopian* which means match everything which starts with "@utopian-bot !utopian". Next is to get the value after "@utopian-bot !utopian" which can be done using the Regex @utopian-bot !utopian *([^\n]*)
. The Explanation from https://regex101.com/ is
([^\n]*)
means get all the characters from the single line and then add it as a group. Thus we will get blank or the beneficiaries i.e. [], or '@mention1 @mention2 @mention3 @mention4' or '@mention1:15% @mention2:35% @mention3:25% @mention4:25%'.
Our Next Part is to get the mention and the appropriate weight of that mentions which can be done using the regex (\w+(?::)(\d+)?)
which is creating groups of mention:weight as shown below
\w+
matches any word character
:
matches the character : literally (case sensitive)
\d+
matches a digit (equal to [0-9])
Now after that you will get a group, thus you have to loop through it and send the mention and weight in the beneficiary. This is a how you approach for the similar Task Request in teh future.