Exploring Regex in Construct 2

6

Index

Attached Files

The following files have been attached to this tutorial:

.capx

indiekiwi-regex-tutorial.capx

Download now 172.11 KB

Stats

11,359 visits, 27,421 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.

Published on 9 Jan, 2015. Last updated 19 Feb, 2019

Expression breakdown

For the purpose of this tutorial, I'll just explain the key expression components that you are likely to need in C2 and game development. If you would like to learn more, regular-expressions.info is the perfect place to begin and regexpal.com is the perfect place to practice and test.

Heres the Expression again for reference: "[\d\w]+\s([\w\s]+)\s\w+\.?"

.

a fullstop means "any character" and is a wildcard

[/h2] while "\" by itself doesn't mean much, it is the "escape" and a "prefix" to some of regex's expression components so if you want to use one of the characters that also have another meaning to regex, you will need to "escape" it first. ie if you want to match a real "." full stop instead of a "any character wildcard", you should use "\." abcABC123 etc

Typing in alphabetical and numerical characters will match them exactly as they are, these are very helpful for anchoring what you need to extract.

\d

d for digit, this represents one character

\w

w for "word"(?), this represents one alphabetical character

\s

s for space, this allows you to detect a space, you can also physically type in a real space (" ") as well!

A(B)C

The "(B)" is a capture group, notice in our expression, we use it to encapsulate where we expect the street name to be. So when we retrieve the data we can get it without the street number and street domain. If we encapsulated those, we could have multiple captured groups and access them by different indexes

A|B

The "|" is an "OR" seperator, If we quantify it we can have a mixture of both A and B

[A|B]

Check our expression again, we have [\d\w], this means a digit or alphabetical character, The square brakets[] allow us to quantify how many are expected, see the next 4 expression components below about quantifying:

?

This is zero or one, One of our address domains contained a fullstop at the end, some didn't, so we used "\.?" to tell our expression there may or may not be one full stop here.

+

"One or more", quantifier where there is at least one expected match for the previous expression component to look for

*

"Zero or more", quantifier where there may be none, one or more of the previous expression component to look for

{5}

We can specify the exact number of occurrences, 5 in this case

^

This is a hook that will start matching from the beginning of your string (Because you don't need to match the whole string when you are looking for something specific and the pattern is clear enough)

$

Hook to the end of the string

i

Ignore case flag, Don't care if characters are CAPITAL or lowercase.

m

Allow the expression to contain multiple lines. Note that flags apply to the whole expression and in C2 are entered as separate method parameters and can also be a empty string.

Both i and m flags can be used as "im" or "mi".

  • 0 Comments

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