-
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 b386863
Showing
5 changed files
with
100 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
55 changes: 55 additions & 0 deletions
55
examples/regexp-find-operations/regexp_find_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,55 @@ | ||
import ballerina/io; | ||
import ballerina/lang.regexp; | ||
|
||
function printGroupsWithinLog(regexp:Groups logGroup) { | ||
// The first element in the `logGroup` is the entire matched string. | ||
// The subsequent elements in `logGroup` represent the captured groups | ||
// (timestamp, component, message). | ||
string timestamp = (<regexp:Span>logGroup[1]).substring(); | ||
string component = (<regexp:Span>logGroup[2]).substring(); | ||
string logMessage = (<regexp:Span>logGroup[3]).substring(); | ||
|
||
io:println(string `Timestamp: ${timestamp}`); | ||
io:println(string `Component: ${component}`); | ||
io:println(string `Message: ${logMessage}`); | ||
} | ||
|
||
public function main() { | ||
string logContent = string ` | ||
2024-09-19 10:02:01 WARN [UserLogin] - Failed login attempt for user: johndoe | ||
2024-09-19 10:03:17 ERROR [Database] - Connection to database timed out | ||
2024-09-19 10:04:05 WARN [RequestHandler] - Response time exceeded threshold for /api/v1/users | ||
2024-09-19 10:05:45 INFO [Scheduler] - Scheduled task started: Data backup | ||
2024-09-19 10:06:10 ERROR [Scheduler] - Failed to start data backup: Permission denied | ||
2024-09-19 10:11:55 INFO [Security] - Security scan completed, no issues found | ||
2024-09-19 10:12:30 ERROR [RequestHandler] - 404 Not Found: /api/v1/products`; | ||
|
||
// Regex to match error logs with three groups: | ||
// 1. Timestamp (e.g., 2024-09-19 10:03:17). | ||
// 2. Component (e.g., Database, Scheduler). | ||
// 3. Log message (e.g., Connection to database timed out). | ||
string:RegExp errorLogPattern = re `(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) ERROR \[(\w+)\] - (.*)`; | ||
|
||
// Retrieving the first error log from the `logContent`. | ||
regexp:Span firstErrorLog = <regexp:Span>errorLogPattern.find(logContent); | ||
io:println(string `First error log: ${firstErrorLog.substring()}`); | ||
|
||
// Retrieving all error logs from the `logContent`. | ||
regexp:Span[] allErrorLogs = errorLogPattern.findAll(logContent); | ||
io:println("All error logs:"); | ||
foreach regexp:Span errorLog in allErrorLogs { | ||
io:println(errorLog.substring()); | ||
} | ||
|
||
// Retrieving groups (timestamp, component, message) from the first error log. | ||
regexp:Groups firstErrorLogGroups = <regexp:Groups>errorLogPattern.findGroups(logContent); | ||
io:println("\nGroups within first error log:"); | ||
printGroupsWithinLog(firstErrorLogGroups); | ||
|
||
// Retrieving groups from all error logs. | ||
regexp:Groups[] allErrorLogGroups = errorLogPattern.findAllGroups(logContent); | ||
io:println("\nGroups in all error logs"); | ||
foreach regexp:Groups logGroup in allErrorLogGroups { | ||
printGroupsWithinLog(logGroup); | ||
} | ||
} |
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,13 @@ | ||
# RegExp find operations | ||
|
||
The `RegExp` type provides a set of language library functions to find patterns within strings. These functions enable efficient pattern matching, grouping, and extraction based on specific regular expressions. | ||
|
||
|
||
::: code regexp_find_operations.bal ::: | ||
|
||
::: out regexp_find_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-find-operations/regexp_find_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 find operations. | ||
keywords: ballerina, ballerina by example, bbe, regexp, RegExp, regex, regular expressions, ballerina regex functions, regexp langlib functions, find, findAll, findGroups, findAllGroups |
23 changes: 23 additions & 0 deletions
23
examples/regexp-find-operations/regexp_find_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,23 @@ | ||
$ bal run regexp_find_operations.bal | ||
First error log: 2024-09-19 10:03:17 ERROR [Database] - Connection to database timed out | ||
All error logs: | ||
2024-09-19 10:03:17 ERROR [Database] - Connection to database timed out | ||
2024-09-19 10:06:10 ERROR [Scheduler] - Failed to start data backup: Permission denied | ||
2024-09-19 10:12:30 ERROR [RequestHandler] - 404 Not Found: /api/v1/products | ||
|
||
Groups within first error log: | ||
Timestamp: 2024-09-19 10:03:17 | ||
Component: Database | ||
Message: Connection to database timed out | ||
|
||
Groups in all error logs | ||
Timestamp: 2024-09-19 10:03:17 | ||
Component: Database | ||
Message: Connection to database timed out | ||
Timestamp: 2024-09-19 10:06:10 | ||
Component: Scheduler | ||
Message: Failed to start data backup: Permission denied | ||
Timestamp: 2024-09-19 10:12:30 | ||
Component: RequestHandler | ||
Message: 404 Not Found: /api/v1/products | ||
➜ |