Skip to content

Commit 1ce8276

Browse files
Merge pull request #376 from ballerina-platform/build
Add proposals
2 parents 4d8d701 + 7ccd7b2 commit 1ce8276

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Proposal: Introduce APIs to extract a substring/s from the given string
2+
3+
_Owners_: @daneshk @kalaiyarasiganeshalingam
4+
_Reviewers_: @daneshk
5+
_Created_: 2022/03/11
6+
_Updated_: 2022/03/11
7+
_Issues_: [#2689](https://github.com/ballerina-platform/ballerina-standard-library/issues/2689)
8+
9+
## Summary
10+
The Ballerina Regex module doesn't have any API to extract a substring/s from the given string by using regex. So, this proposal introduces new APIs to extract a substring/s from the given string using regex.
11+
12+
## Goals
13+
Provide a way to extract a substring/s using regex.
14+
15+
## Motivation
16+
As mentioned in the summary, at the moment, users have no way to extract a substring/s from the given string. But this is an important feature in string manipulation. So, this allows users to extract a substring/s easily by using Ballerina.
17+
18+
## Description
19+
20+
```ballerina
21+
# Holds the matched substring and its position in the input string.
22+
#
23+
# + matched - Matched substring
24+
# + startIndex - The start index of the match
25+
# + endIndex - The last index of the match
26+
type PartMatch record {|
27+
string matched;
28+
int startIndex;
29+
int endIndex;
30+
|};
31+
```
32+
33+
```ballerina
34+
# Holds information about matched regex groups
35+
public type Groups readonly & object {
36+
int count;
37+
// Capture groups are indexed from 1
38+
// Group 0 means whole regex
39+
// Panics if i < 0 or > count
40+
isolated function get(int i) returns PartMatch?;
41+
};
42+
```
43+
44+
```ballerina
45+
# Holds the results of a match against a regular expression.
46+
# It contains the match boundaries, groups and group boundaries.
47+
#
48+
# + groups - Information about matched regex groups
49+
public type Match record {|
50+
// The match for the whole regex
51+
*PartMatch;
52+
Groups groups;
53+
|};
54+
```
55+
56+
```ballerina
57+
# Returns the first substring in str that matches the regex.
58+
# ```ballerina
59+
# regex:Match? result = regex:search("Betty Botter bought some butter but she said the butter’s bitter.",
60+
# "\\b[bB].tt[a-z]*");
61+
# ```
62+
#
63+
# + str - The string to be matched
64+
# + regex - The regex value
65+
# + startIndex - The starting index for the search
66+
# + return - a `Match` record which holds the matched substring, or nil if there is no match
67+
public isolated function search(string str, string regex, int startIndex = 0) returns Match?;
68+
```
69+
70+
```ballerina
71+
# Returns all substrings in str that match the regex.
72+
# ```ballerina
73+
# regex:Match[] result = regex:searchAll("Betty Botter bought some butter but she said the butter’s bitter.",
74+
# "\\b[bB].tt[a-z]*");
75+
# ```
76+
#
77+
# + str - The string to be matched
78+
# + regex - The regex value
79+
# + return - An array of `Match` records
80+
# Each member holds a matched substring
81+
public isolated function searchAll(string str, string regex) returns Match[];
82+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Proposal: Improve the replacement functions
2+
3+
_Owners_: @daneshk @kalaiyarasiganeshalingam
4+
_Reviewers_: @daneshk
5+
_Created_: 2022/03/11
6+
_Updated_: 2022/03/11
7+
_Issues_: [#2772](https://github.com/ballerina-platform/ballerina-standard-library/issues/2772)
8+
9+
## Summary
10+
The existing replacement functions don't support the following functionalities:
11+
- There is no way to replace the matches from the particular string index of the given string.
12+
- There isn't any mechanism to replace the matches with a dynamic value.
13+
14+
So, this proposal improves the replacement functions to support the above functionalities.
15+
16+
## Goals
17+
Provide a way to support the above functionalities.
18+
19+
## Motivation
20+
This allows users to get the above functionalities when using replacement functions.
21+
22+
## Description
23+
API improvements are:
24+
25+
- Introduce a function to create the new substring to be used to replace the matches.
26+
```ballerina
27+
# A function to be invoked to create the new substring to be used to replace the matches.
28+
type ReplacerFunction function(Match matched) returns string;
29+
```
30+
31+
- Introduce a new type to get the replacement value.
32+
```ballerina
33+
# A function to be invoked to create the new substring or string value to be used to replace the matches.
34+
public type Replacement ReplacerFunction|string;
35+
```
36+
37+
- Change the type of replacement param from string to `Replacement` type.
38+
```ballerina
39+
# Replaces each occurrence of the substrings, which match the provided
40+
# regex from the given original string value with the
41+
# provided replacement string.
42+
# ```ballerina
43+
# string result = regex:replaceAll("Ballerina is great", "\\s+", "_");
44+
# ```
45+
#
46+
# + originalString - The original string to replace the occurrences of the
47+
# substrings that match the provided regex
48+
# + regex - The regex to match the substrings in the `originalString` , which is to be replaced
49+
# + replacement - The replacement string or function to replace the substrings, which
50+
# match the regex
51+
# + return - The resultant string with the replaced substrings
52+
public isolated function replaceAll(string originalString, string regex, Replacement replacement) returns string;
53+
```
54+
55+
- Introduce a new function to replace the first substring from the given start index of the string.
56+
This is an improved function from the `replaceFirst`.
57+
```ballerina
58+
# Replaces the first substring that matches the given regex with
59+
# the provided replacement string or function.
60+
# ```ballerina
61+
# string result = regex:replace("Ballerina is great", "\\s+", "_");
62+
# ```
63+
#
64+
# + originalString - The original string to replace the first occurrence of the
65+
# substring that matches the provided regex
66+
# + regex - The regex to match the first substring in the `originalString` to
67+
# be replaced
68+
# + replacement - The replacement string or function to replace the first substring, which
69+
# matches the regex
70+
# + startIndex - The starting index for the search
71+
# + return - The resultant string with the replaced substring
72+
public isolated function replace(string originalString, string regex, Replacement replacement, int startIndex = 0)
73+
returns string;
74+
```
75+
76+
- Deprecate the existing `replaceFirst` function.
77+
```ballerina
78+
# Replaces the first substring that matches the given regex with
79+
# the provided replacement string.
80+
# ```ballerina
81+
# string result = regex:replaceFirst("Ballerina is great", "\\s+", "_");
82+
# ```
83+
#
84+
# + originalString - The original string to replace the first occurrence of the
85+
# substring that matches the provided regex
86+
# + regex - The regex to match the first substring in the `originalString` to
87+
# be replaced
88+
# + replacement - The replacement string to replace the first substring, which
89+
# matches the regex
90+
# + return - The resultant string with the replaced substring
91+
public isolated function replaceFirst(string originalString, string regex, string replacement) returns string;
92+
```

0 commit comments

Comments
 (0)