-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5797 from poorna2152/10.x_replace
[2201.10.x] Add BBE for Regex replace operations
- Loading branch information
Showing
10 changed files
with
50 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/regexp-replace-operations/regexp_replace_operations.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import ballerina/io; | ||
|
||
public function main() { | ||
string creditCardNumber = "1234-5678-9876-5432"; | ||
string:RegExp pattern = re `\d{4}-\d{4}-\d{4}`; | ||
// Replace the first occurrence of the credit card pattern with a masked representation. | ||
string maskedCardNumber = pattern.replace(creditCardNumber, "****-****-****"); | ||
io:println(maskedCardNumber); | ||
|
||
xml xmlString = | ||
xml `<root><!--comment 1 --><e1>value1</e1><e2>value2</e2><!--comment 2-->></root>`; | ||
|
||
// Regular expression to match XML comments. | ||
string:RegExp commentPattern = re `<!--.*?-->`; | ||
// Replace all occurrences of XML comments with an empty string, effectively removing them. | ||
string commentsRemovedXml = commentPattern.replaceAll(xmlString.toString(), ""); | ||
io:println("XML string with comments removed: ", commentsRemovedXml); | ||
} |
12 changes: 12 additions & 0 deletions
12
examples/regexp-replace-operations/regexp_replace_operations.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# RegExp operations | ||
|
||
The `RegExp` type supports a set of langlib functions to replace parts of strings that match specific patterns. | ||
|
||
::: code regexp_replace_operations.bal ::: | ||
|
||
::: out regexp_replace_operations.out ::: | ||
|
||
## Related links | ||
- [RegExp type](/learn/by-example/regexp-type) | ||
- [RegExp API Docs](https://lib.ballerina.io/ballerina/lang.regexp) | ||
- [string API Docs](https://lib.ballerina.io/ballerina/lang.string) |
2 changes: 2 additions & 0 deletions
2
examples/regexp-replace-operations/regexp_replace_operations.metatags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates how to use the regexp langlib functions relevant to regex replace operations. | ||
keywords: ballerina, ballerina by example, bbe, regexp, RegExp, regex, regular expressions, ballerina regex functions, regexp langlib functions, replace, replaceAll |
3 changes: 3 additions & 0 deletions
3
examples/regexp-replace-operations/regexp_replace_operations.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$ bal run regexp_replace_operations.bal | ||
****-****-****-5432 | ||
XML string with comments removed: <root><e1>value1</e1><e2>value2</e2>></root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...s/regexp-operations/regexp_operations.out → ...s_overview/regexp_operations_overview.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
$ bal run regexp_operations.bal | ||
$ bal run regexp_operations_overview.bal | ||
["bob@example.net","alice@example.com","charlie@example.org","david@example.xyz","invalid#@example.n%t"] | ||
["bob","alice","charlie","david"] | ||
["example.net","example.com","example.org","example.xyz"] |