Skip to content

Commit

Permalink
Address review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Oct 21, 2024
1 parent 87e14b6 commit bac1a43
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 18 deletions.
4 changes: 2 additions & 2 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,8 @@
"isLearnByExample": true
},
{
"name": "RegExp operations",
"url": "regexp-operations",
"name": "RegExp operations overview",
"url": "regexp_operations_overview",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
Expand Down
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
19 changes: 7 additions & 12 deletions examples/regexp-replace-operations/regexp_replace_operations.bal
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ import ballerina/io;
public function main() {
string creditCardNumber = "1234-5678-9876-5432";
string:RegExp pattern = re `(\d{4})-(\d{4})-(\d{4})`;
// Replaces the first occurrence of the credit card pattern with a masked representation.
// Replace the first occurrence of the credit card pattern with a masked representation.
string maskedCardNumber = pattern.replace(creditCardNumber, "****-****-****");
io:println(maskedCardNumber);

string xmlString = "<root>" +
"<!-- This is a comment -->" +
"<element1>Value 1</element1>" +
"<element2>Value 2</element2>" +
"<!-- Another comment -->" +
"<element3>Value 3</element3>" +
"</root>";
xml xmlString =
xml `<root><!--comment 1 --><e1>value1</e1><e2>value2</e2><!--comment 2-->></root>`;

// Regular expression to match XML comments
// Regular expression to match XML comments.
string:RegExp commentPattern = re `<!--.*?-->`;
// Replaces all occurrences of XML comments with an empty string, effectively removing them.
string commentsRemoved = commentPattern.replaceAll(xmlString, "");
io:println(string `Comments removed XML: ${commentsRemoved}`);
// 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);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# RegExp operations

The ``RegExp`` type supports a set of anguage library functions for replacing patterns in strings.
The `RegExp` type supports a set of langlib functions to replace parts of strings that match specific patterns.

::: code regexp_replace_operations.bal :::

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
$ bal run regexp_replace_operations.bal
****-****-****-5432
Comments removed XML: <root><element1>Value 1</element1><element2>Value 2</element2><element3>Value 3</element3></root>
XML string with comments removed: <root><e1>value1</e1><e2>value2</e2>&gt;</root>

0 comments on commit bac1a43

Please sign in to comment.