Skip to content

Commit a21dfd8

Browse files
Merge pull request #380 from ballerina-platform/regex
Handles regex special cases
2 parents 8872100 + b5ddac3 commit a21dfd8

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

ballerina/natives.bal

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import ballerina/jballerina.java;
1818
import ballerina/lang.'string as strings;
1919

2020
# Checks whether the given string matches the provided regex.
21+
# Note that `\\` is used as for escape sequence and `\\\\` is used to inserts a backslash
22+
# character in the string or regular expression.
2123
# ```ballerina
2224
# boolean isMatched = regex:matches("Ballerina is great", "Ba[a-z ]+");
2325
# ```
@@ -31,6 +33,8 @@ public isolated function matches(string stringToMatch, string regex) returns boo
3133

3234
# Replaces the first substring that matches the given regex with
3335
# the provided replacement string or string returned by the provided function.
36+
# Note that `\\` is used as for escape sequence and `\\\\` is used to inserts a backslash
37+
# character in the string or regular expression.
3438
# ```ballerina
3539
# string result = regex:replace("Ballerina is great", "\\s+", "_");
3640
# ```
@@ -60,8 +64,9 @@ public isolated function replace(string originalString, string regex, Replacemen
6064
}
6165

6266
# Replaces each occurrence of the substrings, which match the provided
63-
# regex from the given original string value with the
64-
# provided replacement string.
67+
# regex from the given original string value with the provided replacement string.
68+
# Note that `\\` is used as for escape sequence and `\\\\` is used to inserts a backslash
69+
# character in the string or regular expression.
6570
# ```ballerina
6671
# string result = regex:replaceAll("Ballerina is great", "\\s+", "_");
6772
# ```
@@ -92,6 +97,8 @@ public isolated function replaceAll(string originalString, string regex, Replace
9297

9398
# Replaces the first substring that matches the given regex with
9499
# the provided replacement string.
100+
# Note that `\\` is used as for escape sequence and `\\\\` is used to inserts a backslash
101+
# character in the string or regular expression.
95102
# ```ballerina
96103
# string result = regex:replaceFirst("Ballerina is great", "\\s+", "_");
97104
# ```
@@ -107,20 +114,13 @@ public isolated function replaceAll(string originalString, string regex, Replace
107114
# This function will be removed in a later. Use `replace` instead.
108115
@deprecated
109116
public isolated function replaceFirst(string originalString, string regex, string replacement) returns string {
110-
handle|error value = trap replaceFirstExternal(java:fromString(originalString), java:fromString(regex),
111-
java:fromString(replacement));
112-
if value is handle {
113-
string? updatedString = java:toString(value);
114-
if updatedString is string {
115-
return updatedString;
116-
}
117-
panic error(string `error occurred while replacing ${regex} in ${originalString}`);
118-
}
119-
panic error(string `error occurred while replacing ${regex} in ${originalString}: ` + value.detail().toString());
117+
return replace(originalString, regex, replacement);
120118
}
121119

122120
# Returns an array of strings by splitting a string using the provided
123121
# regex as the delimiter.
122+
# Note that `\\` is used as for escape sequence and `\\\\` is used to inserts a backslash
123+
# character in the string or regular expression.
124124
# ```ballerina
125125
# string[] result = regex:split("Ballerina is great", " ");
126126
# ```
@@ -134,6 +134,8 @@ public isolated function split(string receiver, string delimiter) returns string
134134
}
135135

136136
# Returns the first substring in str that matches the regex.
137+
# Note that `\\` is used as for escape sequence and `\\\\` is used to inserts a backslash
138+
# character in the string or regular expression.
137139
# ```ballerina
138140
# regex:Match? result = regex:search("Betty Botter bought some butter but she said the butter’s bitter.",
139141
# "\\b[bB].tt[a-z]*");
@@ -161,6 +163,8 @@ public isolated function search(string str, string regex, int startIndex = 0) re
161163
}
162164

163165
# Returns all substrings in string that match the regex.
166+
# Note that `\\` is used as for escape sequence and `\\\\` is used to inserts a backslash
167+
# character in the string or regular expression.
164168
# ```ballerina
165169
# regex:Match[] result = regex:searchAll("Betty Botter bought some butter but she said the butter’s bitter.",
166170
# "\\b[bB].tt[a-z]*");

ballerina/tests/regex-test.bal

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ isolated function testReplaceFirst() {
8989
test:assertEquals(actualvalue, "Replace ThisTextThis", msg = "String values are not equal");
9090
}
9191

92+
@test:Config {}
93+
isolated function testReplaceFirst1() {
94+
string original = "ReplaceThisThisTextThis";
95+
string regex = "This";
96+
string replacement = "$ ";
97+
string actualvalue = replaceFirst(original, regex, replacement);
98+
test:assertEquals(actualvalue, "Replace$ ThisTextThis", msg = "String values are not equal");
99+
}
100+
101+
@test:Config {}
102+
isolated function testReplaceFirst2() {
103+
string original = "ReplaceThisThisTextThis";
104+
string regex = "This";
105+
string replacement = "$1 ";
106+
string actualvalue = replaceFirst(original, regex, replacement);
107+
test:assertEquals(actualvalue, "Replace$1 ThisTextThis", msg = "String values are not equal");
108+
}
109+
110+
@test:Config {}
111+
isolated function testReplaceFirst3() {
112+
string original = "ReplaceThisThisTextThis";
113+
string regex = "This";
114+
string replacement = "$this ";
115+
string actualvalue = replaceFirst(original, regex, replacement);
116+
test:assertEquals(actualvalue, "Replace$this ThisTextThis", msg = "String values are not equal");
117+
}
118+
92119
@test:Config {}
93120
isolated function testReplace() {
94121
string original = "10010011";

0 commit comments

Comments
 (0)