Skip to content

Instantly share code, notes, and snippets.

@IanSSenne
Last active February 22, 2023 06:44
Show Gist options
  • Save IanSSenne/0e4f1c7c4ec3c4ac2f94ecc8868de127 to your computer and use it in GitHub Desktop.
Save IanSSenne/0e4f1c7c4ec3c4ac2f94ecc8868de127 to your computer and use it in GitHub Desktop.
minecraft position argument regex

Minecraft Like Command Parsing Using Regex

In this article I will talk about how I attempted to parse the position argument of a minecraft command using regex as well as describe the regex I used to do so.

Summary

During my time working with the gametest framework for minecraft I wrote a small framework for building out custom commands. One feature sometimes used is the ability to specify a position in the world for the command to use. to do this I used a regex to locate a portion of the syntax needed to specify a position. The regular expression /^([~^]-?\d*(?:\.\d+)?|-?\d+(?:\.\d+)?)/ has a couple of parts where it looks for a prefix, and then a partial or complete number, minecraft supports 2 prefix, ~ and ^ which are used to specify a relative position, and an absolute position respectively. This regex also supports negative numbers, and numbers with decimal points as well as numbers without a prefix.

Table of Contents

Regex Components

Anchors

In this regular expression the only anchor used is to specify the start of the string ^. This is used to ensure that the regex matches from the start of the string, In the context this was used used we removed the non relevent portion of the string before running it to aquire the value from the start of the string which could then be removed before running another pass.

Quantifiers

This regular exprssion uses several quantifiers, *, ?, and +.

The * quantifier is used to match the previous character 0 or more times. This is used to to see if there is a number after a specifier, if there isnt my code would assume a value of 0 but this is allowed by the regex to more closely respect the location syntax minecraft uses.

The ? quantifier is used to match the previous character 0 or 1 times. This is used to match any decimal value after the number if there is one.

The + quantifier is used to match the previous character 1 or more times. This is used when matching a decimal to enforce you have at least one digit after the decimal point.

Grouping Constructs

This regular expression uses both capturing () and non capturing (?:) groups. The capturing groups are used to capture the entire result of the regex and surrounds the entire thing except for the start anchor. The non capturing groups are used to group together the expression for the decimal portion of the number so that it can be made optional.

Bracket Expressions

near the start of the regex there is a bracket expression [~^] this is used to match any of the characters ~ or ^ and is intended to match the prefix of the number if one is present. A note about this is that they have to appear in this order, if the ^ was first then it would become a negated character class and would match any character that was not ~.

The OR Operator

The OR Operator | is used to seperate the portions of this regular expression that match a pure numeric value and a prefixed value as the prefixed value has slightly different requirements.

Flags

In Production this regular expression does not use any flags. this is due to the nature of only looking for a single match at the start of the string and not accepting any alphanumeric characters. If it did accept alphanumeric characters then the i flag may be relevant to allow for case insensitivity.

Character Escapes

The only character escape used is \d which is used to match any digit character. This is a shorthand for `[0-9]

Author

Hi, I'm Ian and I like to use typescript and create minecraft related tools. You can view my github if you would like to explore.

Github: link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment