diff --git a/examples/index.json b/examples/index.json index 97ab04bc67..f0d6ec820f 100644 --- a/examples/index.json +++ b/examples/index.json @@ -1218,8 +1218,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 diff --git a/examples/regexp-operations/regexp_operations.bal b/examples/regexp-operations-overview/regexp_operations_overview.bal similarity index 100% rename from examples/regexp-operations/regexp_operations.bal rename to examples/regexp-operations-overview/regexp_operations_overview.bal diff --git a/examples/regexp-operations/regexp_operations.md b/examples/regexp-operations-overview/regexp_operations_overview.md similarity index 78% rename from examples/regexp-operations/regexp_operations.md rename to examples/regexp-operations-overview/regexp_operations_overview.md index ae4bf653f9..8843f7712d 100644 --- a/examples/regexp-operations/regexp_operations.md +++ b/examples/regexp-operations-overview/regexp_operations_overview.md @@ -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) diff --git a/examples/regexp-operations/regexp_operations.metatags b/examples/regexp-operations-overview/regexp_operations_overview.metatags similarity index 100% rename from examples/regexp-operations/regexp_operations.metatags rename to examples/regexp-operations-overview/regexp_operations_overview.metatags diff --git a/examples/regexp-operations/regexp_operations.out b/examples/regexp-operations-overview/regexp_operations_overview.out similarity index 82% rename from examples/regexp-operations/regexp_operations.out rename to examples/regexp-operations-overview/regexp_operations_overview.out index 97c4630eaf..96c43e6822 100644 --- a/examples/regexp-operations/regexp_operations.out +++ b/examples/regexp-operations-overview/regexp_operations_overview.out @@ -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"] diff --git a/examples/regexp-replace-operations/regexp_replace_operations.bal b/examples/regexp-replace-operations/regexp_replace_operations.bal new file mode 100644 index 0000000000..abd6375d9c --- /dev/null +++ b/examples/regexp-replace-operations/regexp_replace_operations.bal @@ -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 `value1value2>`; + + // 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); +} diff --git a/examples/regexp-replace-operations/regexp_replace_operations.md b/examples/regexp-replace-operations/regexp_replace_operations.md new file mode 100644 index 0000000000..c4dd8cf6eb --- /dev/null +++ b/examples/regexp-replace-operations/regexp_replace_operations.md @@ -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) diff --git a/examples/regexp-replace-operations/regexp_replace_operations.metatags b/examples/regexp-replace-operations/regexp_replace_operations.metatags new file mode 100644 index 0000000000..51eae072fa --- /dev/null +++ b/examples/regexp-replace-operations/regexp_replace_operations.metatags @@ -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 diff --git a/examples/regexp-replace-operations/regexp_replace_operations.out b/examples/regexp-replace-operations/regexp_replace_operations.out new file mode 100644 index 0000000000..49d925fb52 --- /dev/null +++ b/examples/regexp-replace-operations/regexp_replace_operations.out @@ -0,0 +1,3 @@ +$ bal run regexp_replace_operations.bal +****-****-****-5432 +XML string with comments removed: value1value2> diff --git a/examples/regexp-type/regexp_type.md b/examples/regexp-type/regexp_type.md index d3607f1bba..683d7935ab 100644 --- a/examples/regexp-type/regexp_type.md +++ b/examples/regexp-type/regexp_type.md @@ -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)