Exploring Regex in Construct 2

Index

Attached Files

The following files have been attached to this tutorial:

.capx

indiekiwi-regex-tutorial.capx

Download now 172 KB

Stats

3,672 visits, 10,718 views

Translations

This tutorial hasn't been translated.

Tools

RegexMatchAt

Documentation

    RegexMatchAt(String, Regex, Flags, Index)
    Process the regular expression Regex on String with Flags, and in the list of results, return the entry at Index.

Usage

This method will help extract strings like we saw at the beginning of this tutorial. We can feed each address in a loop as the first parameter "String".

Parameters

* String - Your string that you need to extract from

* Regex - The regular expression pattern. "[\d\w]+\s([\w\s]+)\s\w+\.?"

* Flags - A flag that will apply an effect to the pattern behavior

Index - Index 0 contains everything matched, this is the full street address. Index 1 will contain our first capture group which has captured the Street name as "() " was used to surround the street name

Image!

Result

apple banana Water Melon

RegexMatchCount

Documentation

    RegexMatchCount(String, Regex, Flags)
    Process the regular expression Regex on String with Flags, and return the number of entries in the list of results.

Usage

This will count how many times our pattern will match our string, Since we crafted our regex to perfectly fit one string, lets join up the three addresses and see how many matches we get.

Parameters

* String - The string that will be used for counting the number of matching occurrences for the regex. We're using "123 apple St. 1A banana Ave 43 Water Melon Park"

* Regex - the pattern, We'll use the same one as before. "[\d\w]+\s([\w\s]+)\s\w+\.?"

* Flags - We'll need to use "i" to make it case insensitive!

Did you notice that we don't have the "Index" parameter here? So if we need to find out how many parameters are set for the RegexMatchAt, we can use this RegexMatchCount method to check if the index is set to avoid a null pointer.

Image!

Result

2... You may be wondering why we didn't get 3? This is because we built our regex for another purpose, we knew our boundaries previously and it was satisfactory for RegexMatchAt. Now that we combined three strings, the pattern matched the second and third addresses as one match.

RegexReplace

Documentation

    RegexReplace(String, Regex, Flags, Replace)
    In String substitute matches for the regular expression Regex (with Flags) with the string Replace. The replacement string can contain the following special characters: $$ (inserts a $), $& (inserts the matched substring), $` (inserts the portion of the string that precedes the matched substring), or $' (inserts the portion of the string that follows the matched substring).

Usage

This will use the regex pattern and replace it with a new string for every pattern match in the string. We need to use a new pattern now, because if we use the old one, it will replace everything as that pattern matches the whole address at index 0. We'll be using \s\w+\s which basically means any whole word (with spaces at the beginning and end of the word) will be matched.

Parameters

* String - We'll process them one by one, we'll be using "123 apple St.", "1A banana Ave 43", "Water Melon Park"

* Regex - We'll be using "\s\w+\s" as described earlier

* Flags - Using "i" for case insensitive again.

* Replace - This is a string that will replace every occurance that was found in the original String, the match is removed and this is inserted into the replacements of the original input String

Image!

Result

"123KiwiSt. 1AKiwiAve 43KiwiMelon Park"

RegexSearch

Documentation

    RegexSearch(String, Regex, Flags)
    Return the index of the first character in String where a match for Regex with Flags could be found.

Usage

This method will count from the left, how many indexes it takes before it matches it's first occurrence of the regex pattern

Parameters

String - Input, Again, we'll be using them in a loop seperately "123 apple St.", "1A banana Ave 43", "Water Melon Park"

Regex - We'll use the same regex as our last example in RegexReplace, as our result showed that "Kiwi" appeared at the 4th, 3rd and 3rd indexes, our result in this method is expected to be "3 2 2" with regex: "\s\w+\s" (The index starts from 0!)

Image!

Result

"3 2 2", as expected, it gave us the index of where the pattern started from the first match for each of our 3 streets.

Wrapping up

The quoted methods and descriptions are borrowed from the documentation in Ashley's System expressions tutorial

Our regex example used is a little bit complicated, they can be a whole lot easier and a whole lot harder too. It did help me to explain a lot of I wanted to cover, so I've attached a capx so you can play around with it as well.

.CAPX

indiekiwi-regex-tutorial.capx

Download now 172 KB
  • 0 Comments

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