You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/spec/spec.md
+83-12Lines changed: 83 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,9 @@ The conforming implementation of the specification is released and included in t
20
20
1.[Overview](#1-overview)
21
21
2.[Operations](#2-operations)
22
22
* 2.1. [Matches](#21-matches)
23
-
* 2.2. [Replace All](#22-replace-all)
24
-
* 2.3. [Replace First](#23-replace-first)
25
-
* 2.4. [Split](#24-split)
23
+
* 2.2. [Replace](#22-replace)
24
+
* 2.3. [Split](#23-split)
25
+
* 2.4. [Search](#24-search)
26
26
27
27
# 1. Overview
28
28
This library is based on [regular expressions](https://en.wikipedia.org/wiki/Regular_expression), which are notations
@@ -36,22 +36,93 @@ This is used to check whether a string matches the provided regex.
36
36
public isolated function matches(string stringToMatch, string regex) returns boolean;
37
37
```
38
38
39
-
## 2.2. Replace All
40
-
This replaces all occurrences of substrings that matches the provided regex in the original string with the provided
41
-
replacement string.
39
+
## 2.2. Replace
40
+
41
+
The replace APIs are used to replace the occurrence/s of substrings that matches the provided regex in the original
42
+
string with the provided replacement string or string returned by the provided function. The following function and
43
+
type are used to provide the constant or dynamic replacement string.
44
+
45
+
```ballerina
46
+
# A function to be invoked to create the new substring to be used to replace the matches.
47
+
type ReplacerFunction function(Match matched) returns string;
48
+
```
49
+
50
+
```ballerina
51
+
# A function to be invoked to create the new substring or string value to be used to replace the matches.
52
+
public type Replacement ReplacerFunction|string;
53
+
```
54
+
55
+
The following APIs are provided to replace the matches:
56
+
57
+
- To replace all occurrences of substrings that matches the provided regex in the original string with the provided
58
+
replacement string or string returned by the provided function.
42
59
```ballerina
43
-
public isolated function replaceAll(string originalString, string regex, string replacement) returns string;
60
+
public isolated function replaceAll(string originalString, string regex, Replacement replacement) returns string;
44
61
```
45
62
46
-
## 2.3. Replace First
47
-
This replaces only the first occurrence of the substring that matches the provided regex in the original string with
48
-
the provided replacement string.
63
+
- To replace only the first occurrence of the substring from the start index that matches the provided regex in the original string with
64
+
the provided replacement string or string returned by the provided function.
49
65
```ballerina
50
-
public isolated function replaceFirst(string originalString, string regex, string replacement) returns string;
66
+
public isolated function replace(string originalString, string regex, Replacement replacement, int startIndex = 0) returns string;
51
67
```
52
68
53
-
## 2.4. Split
69
+
## 2.3. Split
54
70
This splits a string into an array of substrings, using the provided regex as the delimiter.
55
71
```ballerina
56
72
public isolated function split(string receiver, string delimiter) returns string[];
57
73
```
74
+
75
+
## 2.4. Search
76
+
77
+
The search APIs extract substring/s of the string that matches the provided regex. It provides details of the matches such as substring value, start index, end index, and matched regex groups
78
+
79
+
The following records are used to hold the results of a match against a regular expression.
80
+
81
+
```ballerina
82
+
# Holds the matched substring and its position in the input string.
83
+
#
84
+
# + matched - Matched substring
85
+
# + startIndex - The start index of the match
86
+
# + endIndex - The last index of the match
87
+
type PartMatch record {|
88
+
string matched;
89
+
int startIndex;
90
+
int endIndex;
91
+
|};
92
+
```
93
+
94
+
```ballerina
95
+
# Holds the results of a match against a regular expression.
96
+
# It contains the match boundaries, groups and group boundaries.
97
+
#
98
+
# + groups - Information about matched regex groups
99
+
public type Match record {|
100
+
// The match for the whole regex
101
+
*PartMatch;
102
+
Groups groups;
103
+
|};
104
+
```
105
+
106
+
This `Groups` object handles the matches with the group of regex.
107
+
108
+
```ballerina
109
+
# Holds information about matched regex groups
110
+
public type Groups readonly & object {
111
+
int count;
112
+
// Capture groups are indexed from 1
113
+
// Group 0 means whole regex
114
+
// Panics if i < 0 or > count
115
+
isolated function get(int i) returns PartMatch?;
116
+
};
117
+
```
118
+
119
+
The following APIs are provided by the regex module to extract string/s.
120
+
- To get all substrings in string that match the regex.
121
+
```ballerina
122
+
public isolated function searchAll(string str, string regex) returns Match[];
123
+
```
124
+
125
+
- To get the first substring from the start index in the given string that matches the regex.
126
+
```ballerina
127
+
public isolated function search(string str, string regex, int startIndex = 0) returns Match?;
0 commit comments