Skip to content

Commit a0ddf00

Browse files
authored
Merge pull request #379 from ballerina-platform/dev
Update the spec
2 parents 79491b1 + fbfda0f commit a0ddf00

File tree

1 file changed

+83
-12
lines changed

1 file changed

+83
-12
lines changed

docs/spec/spec.md

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ The conforming implementation of the specification is released and included in t
2020
1. [Overview](#1-overview)
2121
2. [Operations](#2-operations)
2222
* 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)
2626

2727
# 1. Overview
2828
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.
3636
public isolated function matches(string stringToMatch, string regex) returns boolean;
3737
```
3838

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.
4259
```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;
4461
```
4562

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.
4965
```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;
5167
```
5268

53-
## 2.4. Split
69+
## 2.3. Split
5470
This splits a string into an array of substrings, using the provided regex as the delimiter.
5571
```ballerina
5672
public isolated function split(string receiver, string delimiter) returns string[];
5773
```
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?;
128+
```

0 commit comments

Comments
 (0)