-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7813da
commit 51c677c
Showing
5 changed files
with
47 additions
and
0 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
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
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. | ||
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>"; | ||
|
||
// 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}`); | ||
} |
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 anguage library functions for replacing patterns in strings. | ||
|
||
::: 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 | ||
Comments removed XML: <root><element1>Value 1</element1><element2>Value 2</element2><element3>Value 3</element3></root> |