Skip to content

Commit

Permalink
Merge pull request #5678 from poorna2152/regex_match
Browse files Browse the repository at this point in the history
Add BBE for `Regex` match operations
  • Loading branch information
gimantha authored Oct 28, 2024
2 parents 90e751c + 0640e41 commit b1abe2a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,13 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "RegExp match operations",
"url": "regexp-match-operations",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
}
]
},
Expand Down
53 changes: 53 additions & 0 deletions examples/regexp-match-operations/regexp_match_operations.bal
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 examples/regexp-match-operations/regexp_match_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 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)
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 examples/regexp-match-operations/regexp_match_operations.out
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

0 comments on commit b1abe2a

Please sign in to comment.