Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2201.10.x] Add BBE for Regex replace operations #5797

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1211,8 +1211,15 @@
"isLearnByExample": true
},
{
"name": "RegExp operations",
"url": "regexp-operations",
"name": "RegExp operations overview",
"url": "regexp_operations_overview",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "RegExp replace operations",
"url": "regexp-replace-operations",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
Expand Down
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 examples/regexp-replace-operations/regexp_replace_operations.md
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)
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
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>&gt;</root>
7 changes: 3 additions & 4 deletions examples/regexp-type/regexp_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ A `RegExp` value can be created by using the regexp template expression or calli

## Related links
- [Ballerina regular expression grammar](https://ballerina.io/spec/lang/master/#section_10.1)
- [RegExp langlib functions](/learn/by-example/regexp-operations)
- [`regexp` langlib API Docs](https://lib.ballerina.io/ballerina/lang.regexp)
- [`string` langlib API Docs](https://lib.ballerina.io/ballerina/lang.string)
- [The `ensureType` function](/learn/by-example/ensureType-function/)
- [RegExp langlib functions overview](/learn/by-example/regexp_operations_overview)
- [RegExp API Docs](https://lib.ballerina.io/ballerina/lang.regexp)
- [string API Docs](https://lib.ballerina.io/ballerina/lang.string)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

The ``RegExp`` type supports a set of langlib functions to search and manipulate string values.

::: code regexp_operations.bal :::
::: code regexp_operations_overview.bal :::

::: out regexp_operations.out :::
::: out regexp_operations_overview.out :::

## Related links
- [RegExp type](/learn/by-example/regexp-type)
Expand Down
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"]
Loading