RegEx


super: Object

A regular expression is a special text string for describing a search pattern. Usually this pattern is used by string searching algorithms for find or find and replace operations on strings, or for input validation.

Events

  • Load() This event is called when the object becames available in the current runtime system.

  • Unload() This event is called when the object has been removed from the current runtime system (but not yet deallocated).

Class Methods

  • func count(pattern: String, string: String, options: Int = 0): Int Returns the number of matches of the regular expression within the specified string.

  • func search(pattern: String, string: String, options: Int = 0): List Returns an array containing all the matches of the regular expression in the string.

  • func ranges(pattern: String, string: String, options: Int = 0): List Returns an array containing all the ranges of the regular expression in the string.

  • func replace(pattern: String, string: String, template: String, options: Int = 0): String Returns a new string containing matching regular expressions replaced with the template string.

Examples

var pattern = "A.C";
var string = "AABCAABCBBABC";
var replacement = "ZZZ";
var options = 0;

var count = RegEx.count(pattern, string, options)
var list = RegEx.search(pattern, string, options)
var ranges = RegEx.ranges(pattern, string, options)
var replace = RegEx.replace(pattern, string, replacement, options)

Console.write("Count: \(count)")
Console.write("List: \(list)")
Console.write("Ranges: \(ranges)")
Console.write("Replace: \(replace)")

Result in Creo Console panel:

Count: 3
List: [ABC,ABC,ABC]
Ranges: [1...3,5...3,10...3]
Replace: AZZZAZZZBBZZZ

More on RegEx syntax can be found at: