Exploring Regex in Construct 2

6
  • 27 favourites

Index

Attached Files

The following files have been attached to this tutorial:

.capx

indiekiwi-regex-tutorial.capx

Download now 172.11 KB

Stats

10,431 visits, 25,871 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

What is regex?

Regular expressions are a way to automate extracting certain parts of a string that share a common pattern.

For example, you need to get a list of street names out of a list of 1000 full addresses. It could take hours if you do this manually or you could use a simple regex pattern to effortlessly extract this data.

A practical example

We have three addresses below. There are a lot of different regex expressions that we could write to extract "apple", "banana" and "Water Melon". You'll need to analyse our dataset as well as test it carefully to create an expression that is reliable enough and yet simple.

    123 apple St.
    1A banana Ave
    43 Water Melon Park

* In our data set we can see that all three addresses start with the number of the street address,

* The second one even contains a letter! ("1A")

* All three street domains are different ("St.", "Ave", "Park")

* The first address ends with a dot ("St.")

* And The last address's street name is made up of 2 words ("Water Melon")

* There are mixed CAPITAL and lower case characters throughout the addresses

    Expression: "[\d\w]+\s([\w\s]+)\s\w+\.?"
    Flag: i
    Output:
    [1] "apple"
    [2] "banana"
    [3] "Water Melon"

Hopefully now you understand the concept and how powerful it is and how it can help you in game development. Next we'll start exploring what this expression actually means.

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!