Regular expression matching and replacement. RegEx compiles a pattern once and supports full match, substring search, numbered capture groups, and global replacement. Link with -lib regex.
Result
RegEx result: the matched text, its position, and the text of each numbered capture group
Operations
GetGroup #
Gets the text captured by a group. Groups are numbered from 1 by their opening '(' left to right; '(?:...)' does not capture. Group 0 is the whole match. In a repeated group the last iteration wins.
method : public : GetGroup(index:Int) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| index | Int | group number |
Return
| Type | Description |
|---|---|
| String | captured text, or Nil if the group did not take part in the match or does not exist |
Example
re := RegEx->New("(\\d+)-(\\d+)");
r := re->FindFirst("call 555-1234 now");
if(r <> Nil) {
r->GetGroup(1)->PrintLine();
r->GetGroup(2)->PrintLine();
};GetGroupStart #
Gets the starting index of the text captured by a group
method : public : GetGroupStart(index:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | group number, 0 is the whole match |
Return
| Type | Description |
|---|---|
| Int | starting index, or -1 if the group did not take part in the match or does not exist |
GetLength #
Gets the string length of the match
method : public : GetLength() ~ IntReturn
| Type | Description |
|---|---|
| Int | string length of the match |
Example
re := RegEx->New("\\w+");
r := re->FindFirst("hello world");
if(r <> Nil) {
Console->PrintLine(r->GetLength());
};GetStart #
Gets the starting index of match
method : public : GetStart() ~ IntReturn
| Type | Description |
|---|---|
| Int | starting index of match |
Example
re := RegEx->New("\\d+");
r := re->FindFirst("price: 99 dollars");
if(r <> Nil) {
Console->PrintLine(r->GetStart());
};GetValue #
Gets the string value
method : public : GetValue() ~ StringReturn
| Type | Description |
|---|---|
| String | string value |
Example
re := RegEx->New("[A-Z][a-z]+");
r := re->FindFirst("The Quick Brown Fox");
if(r <> Nil) {
r->GetValue()->PrintLine();
};GroupCount #
Gets the number of capture groups in the pattern, not counting group 0 (the whole match)
method : public : GroupCount() ~ IntReturn
| Type | Description |
|---|---|
| Int | number of capture groups |
New # constructor
Creates a result without capture groups
New(start:Int, length:Int, value:String)Parameters
| Name | Type | Description |
|---|---|---|
| start | Int | starting index of the match |
| length | Int | length of the match |
| value | String | matched string |
New # constructor
Creates a result with capture groups
New(start:Int, length:Int, value:String, group_starts:Int[], group_values:String[])Parameters
| Name | Type | Description |
|---|---|---|
| start | Int | starting index of the match |
| length | Int | length of the match |
| value | String | matched string |
| group_starts | Int | start index of each group, index 0 is the whole match, -1 if a group did not take part |
| group_values | String | text of each group, index 0 is the whole match, Nil if a group did not take part |