-
Notifications
You must be signed in to change notification settings - Fork 193
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 #5678 from poorna2152/regex_match
Add BBE for `Regex` match operations
- Loading branch information
Showing
5 changed files
with
91 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
53 changes: 53 additions & 0 deletions
53
examples/regexp-match-operations/regexp_match_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,53 @@ | ||
import ballerina/io; | ||
import ballerina/lang.regexp; | ||
|
||
public function main() returns error? { | ||
string[] productCodes = ["ELEC-1234", "FURN-5678", "CLOT-1111", "FOOD-3333", "BAR-123"]; | ||
|
||
// Regular expression to match and validate product codes. | ||
// Format: [PRODUCT_TYPE]-[UNIQUE_ID] | ||
string:RegExp productCodePattern = re `^([A-Z]{4})-(\d{4})$`; | ||
|
||
foreach string productCode in productCodes { | ||
// Check if the product code fully matches the expected format. | ||
boolean isValidProductCode = productCodePattern.isFullMatch(productCode); | ||
io:println(string `Product Code: ${productCode}, Valid: ${isValidProductCode}`); | ||
|
||
// If the product code is invalid, skip further processing. | ||
if !isValidProductCode { | ||
continue; | ||
} | ||
|
||
// For a valid product code, extract the product type and unique ID from the match groups. | ||
regexp:Groups? matchGroups = productCodePattern.fullMatchGroups(productCode); | ||
if matchGroups is regexp:Groups { | ||
// The first member in the `matchGroups` tuple is the entire matched string. | ||
// The subsequent members represent the captured groups | ||
// corresponding to product type and unique ID respectively. | ||
io:println("Product Type: ", | ||
(check matchGroups[1].ensureType(regexp:Span)).substring()); | ||
io:println("Unique ID: ", | ||
(check matchGroups[2].ensureType(regexp:Span)).substring()); | ||
} | ||
} | ||
|
||
// Match product code from a specific starting index in the string. | ||
regexp:Span? productCode = productCodePattern.matchAt( | ||
"Product code: FURN-5678, Product code: CLOT-1234", 39); | ||
if productCode is regexp:Span { | ||
io:println("Matched product: ", productCode.substring()); | ||
} | ||
|
||
// Regular expression to extract the time in the format `HH:MM` from a log string. | ||
string:RegExp timePattern = re `([01][0-9]|2[0-3]):([0-5][0-9])`; | ||
|
||
// Find groups of the matching string from a specified starting index. | ||
regexp:Groups? timeMatchGroups = timePattern.matchGroupsAt( | ||
"Production time: 14:35, Production time: 16:15", 41); | ||
if timeMatchGroups is regexp:Groups { | ||
string hour = (check timeMatchGroups[1].ensureType(regexp:Span)).substring(); | ||
string minutes = (check timeMatchGroups[2].ensureType(regexp:Span)).substring(); | ||
io:println("Production hour: ", hour); | ||
io:println("Production minutes: ", minutes); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
examples/regexp-match-operations/regexp_match_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 match patterns in strings and enable operations such as finding, validating, grouping, and extracting data based on regular expressions. | ||
|
||
::: code regexp_match_operations.bal ::: | ||
|
||
::: out regexp_match_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-match-operations/regexp_match_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 match operations. | ||
keywords: ballerina, ballerina by example, bbe, regexp, RegExp, regex, regular expressions, ballerina regex functions, regexp langlib functions, fullMatchGroups, isFullMatch, matchAt, matchGroupsAt |
17 changes: 17 additions & 0 deletions
17
examples/regexp-match-operations/regexp_match_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,17 @@ | ||
$ bal run regexp_match_operations.bal | ||
Product Code: ELEC-1234, Valid: true | ||
Product Type: ELEC | ||
Unique ID: 1234 | ||
Product Code: FURN-5678, Valid: true | ||
Product Type: FURN | ||
Unique ID: 5678 | ||
Product Code: CLOT-1111, Valid: true | ||
Product Type: CLOT | ||
Unique ID: 1111 | ||
Product Code: FOOD-3333, Valid: true | ||
Product Type: FOOD | ||
Unique ID: 3333 | ||
Product Code: BAR-123, Valid: false | ||
Matched product: CLOT-1234 | ||
Production hour: 16 | ||
Production minutes: 15 |