edicted Blog Banner

edicted

regex

regex-javascript-regular-expression.png

Taking the plunge.

During my coding adventures of the past, Regular Expressions are one of those things that I refused to touch with a ten foot pole. I'd see the crazy nonsensical expression and be like, "Nope, not figuring that out today. Nobody needs that!" Turns out it's pretty useful once you actually figure it out.

image.png

regex is a search parameter

Is your program scanning a document and you need to find specific pieces of info within that document? This is the main use-case. The purpose of regex is to provide a search function with a very specific and powerful way of finding exactly the text we are looking for.

So why did I choose now of all times to figure this out?

I started playing a very difficult multiuser dungeon called HackMUD with the intention of getting back into the coding game. The game is built using Javascript and Mongo Database (an extremely javascript-friendly object-oriented database). In order to play the game the player has to create their own scripts to interact with the command-line and database. So far the scripts I've created are:

  • A cracker for breaking low-level (Tier 1) locks.
  • A garbage disposal for throwing away junk items.
  • A scraper that hacks corporate frontends and finds weak targets to attack.

All of these things can be done manually on the CLI (command-line) but as the player progresses through the game the point is to automate all the tedious bits so they no longer have to be done manually. On a technical level it's very easy to play the game without personally needing to code because other players make their code public and you can run their scripts. However if you run the wrong script you can lose all your money (and sometimes all your items/upgrades as well). Seeing as the entire point was to get back into coding I'm creating my own scripts from scratch and the process has been interesting (and very very slow). I'm also getting a lot of ideas for a game I wanted to create on Hive called Net Ninja. No promises.


image.png

So where does regex come in?

It all started when I saw this in someone else's hackMUD script:

.match(/N(\w+).*$/)

OMG what does it mean though?

Luckily there are really good regex websites that will tell you exactly what the thing does.

image.png

https://regex101.com/

And when I first took a look at this... the website told me exactly what it did... but it still made no sense within the context of the game. What it's doing here is looking for a pair of graves/backticks with the letter N after the first grave. Between the `N` it's looking for a word consisting of alphanumeric characters which is what \w+ means. \w matches any alphanumeric character (including _ underscore apparently) and the + repeats that until it stops finding them. Then the .*$ denotes the end of the string, meaning that this regex can only match once and it will match the one closest to the back.

As if it wasn't confusing enough.

The reason why this was confusing to me within the context of HackMUD is that backticks pairs don't exist. They are invisible on the frontend because they exist as color codes.

image.png image.png
https://hackmud.fandom.com/wiki/Colors

Hm... yep. So the backticks are invisible because they denote a certain color code. Jumped right into the deep end with the confusion on that one. The black box of HackMUD is quite unforgiving... but when you make something that actually works it's pretty fantastic. Not that I would recommend this game to anyone... I almost quit myself (a few times) because of how mind-numbingly difficult it is. Luckily the discord is very active and the entire point and difficulty of the game revolves around the multiplayer aspect.

So it turned out that the `N(word)` regex being searched for is the color code cyan (#00FFFF), which unsurprisingly turns out to be one of the most important colors in the game because it's the color of all locks and arguments given to dictionary keys. This particular regex was finding the type of lock that needs to be cracked. Once I finally understood what it did was I able to incorporate it into my own scripts.

Creating my own regex

password = password.match(/strategy\s(\w+)/)

The corporations in HackMUD are very dumb and they tell you the password to their employees list (hackable targets) from the homepage frontend.

image.png

Some might consider this a spoiler but I don't know the game is so hard and no on here is going to play it so whatever. Navigating to the "about us" page (in this case "info") seems to always provide the password after "We are calling this strategy: ******" The strategy is always the password so I wrote the regex /strategy\s(\w+)/ to grab it. The code finds "strategy" then looks for whitespace \s then grabs the word after it with (\w+) just like in the previous example. Looks like I'm learning regex. If I'm being honest it's almost embarrassing it took me this long and required a faux hacking game to get it done.

Conclusion

Regex is a very powerful (and often confusing) tool for advanced test searching. Seeing as Hive itself is just one big lump of text I'm glad that I'm starting to get a feel for how these searches work. Ultimately I'd like to build a basic game that helps our users learn Javascript and get rewarded while doing it. Maybe I'll even create a Game Design Document for Net Ninja in a post sometime. Dream Big!


Return from regex to edicted's Web3 Blog

regex was published on and last updated on 09 May 2024.