From 1f2b3727b7495db3b89b038b229a58992b3a11d2 Mon Sep 17 00:00:00 2001 From: MaryamZi Date: Mon, 21 Oct 2024 19:36:26 +0530 Subject: [PATCH] Update the RegExp type BBE --- examples/regexp-type/regexp_type.bal | 27 ++++++++++++++++++--------- examples/regexp-type/regexp_type.md | 7 ++++--- examples/regexp-type/regexp_type.out | 2 ++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/examples/regexp-type/regexp_type.bal b/examples/regexp-type/regexp_type.bal index b9f4b9611e..06d2cf6cd3 100644 --- a/examples/regexp-type/regexp_type.bal +++ b/examples/regexp-type/regexp_type.bal @@ -3,10 +3,16 @@ import ballerina/lang.regexp; public function main() returns error? { - // Declare a RegExp using regular expression literal. - string:RegExp _ = re `[a-zA-Z0-9]`; + // Declare a `RegExp` value using a regular expression literal. + regexp:RegExp alphanumericPattern = re `[a-zA-Z0-9]`; + // Matches any string that is a single alphanumeric character. + io:println("Pattern `[a-zA-Z0-9]` matches `a`: ", alphanumericPattern.isFullMatch("a")); - // Declare a RegExp using the `fromString` constructor method. + // `string:RegExp` is an alias for `regexp:RegExp`. + string:RegExp alphanumericPattern2 = re `[a-zA-Z0-9]`; + io:println("Pattern `[a-zA-Z0-9]` matches `a1`: ", alphanumericPattern2.isFullMatch("a1")); + + // Construct a `RegExp` value from a string using the `fromString` langlib function. string:RegExp pattern = check regexp:fromString("HELLO*"); // Matches any string that starts with "HELLO" and ends with zero or more characters. io:println("Pattern `HELLO*` matches `HELLO`: ", pattern.isFullMatch("HELLO")); @@ -15,13 +21,13 @@ public function main() returns error? { boolean result = re `a+`.isFullMatch("aaa"); io:println("Pattern `a+` matches `aaa`: ", result); - // Interpolation can be used to change the pattern dynamically. + // Interpolations can be used to construct the pattern dynamically. string literal = "xyz"; string:RegExp pattern2 = re `abc|${literal}`; // Matches any string that starts with "abc" or "XYZ". io:println("Pattern `abc|${\"xyz\"}` matches `xyz`: ", pattern2.isFullMatch("xyz")); - // RegExp supports writing Unicode general category patterns. + // The `RegExp` type supports Unicode general category patterns. // Characters are matched based on their Unicode properties. string:RegExp pattern3 = re `\p{Ll}`; io:println("Pattern `\\p{Ll}` matches `a`: ", "a".matches(pattern3)); @@ -31,12 +37,15 @@ public function main() returns error? { string:RegExp pattern4 = re `\P{P}`; io:println("Pattern `\\p{P}` matches `0`: ", "0".matches(pattern4)); - // RegExp supports matching characters according to the script using Unicode script patterns. + // The `RegExp` type supports matching characters according to the script using Unicode script patterns. string:RegExp pattern5 = re `\p{sc=Latin}`; - regexp:Span? latinValue = pattern5.find("aεЛ"); - io:println("Pattern `\\p{sc=Latin}` matches `aεЛ`: ", (latinValue).substring()); + regexp:Span? findResult = pattern5.find("aεЛ"); + // The `find` function returns nil if no match is found. Since we know a + // match is found here, use the `ensureType` function to narrow the type down to `regexp:Span`. + regexp:Span latinValue = check findResult.ensureType(); + io:println("Pattern `\\p{sc=Latin}` matches `aεЛ`: ", latinValue.substring()); - // RegExp supports non-capturing groups to control the behavior of regular expression patterns. + // The `RegExp` type supports non-capturing groups to control the behavior of regular expression patterns. // The `i` flag will ignore the case of the pattern. string:RegExp pattern6 = re `(?i:BalleRINA)`; io:println("Pattern `(?i:BalleRINA)` matches `Ballerina`: ", "Ballerina".matches(pattern6)); diff --git a/examples/regexp-type/regexp_type.md b/examples/regexp-type/regexp_type.md index 622a422169..d3607f1bba 100644 --- a/examples/regexp-type/regexp_type.md +++ b/examples/regexp-type/regexp_type.md @@ -4,7 +4,7 @@ The `RegExp` type is defined in the `lang.regexp` module, and can also be referenced using the type alias named the same in the `lang.string` module. The `RegExp` type conforms to a subset of the ECMAScript specification for regular expressions. -A `RegExp` value can be created by using the regexp template expression or calling the `fromString` method of the [lang.regexp](https://lib.ballerina.io/ballerina/lang.regexp/latest#fromString) module. +A `RegExp` value can be created by using the regexp template expression or calling the [`fromString` method of the lang.regexp](https://lib.ballerina.io/ballerina/lang.regexp/latest#fromString) module. ::: code regexp_type.bal ::: @@ -13,5 +13,6 @@ A `RegExp` value can be created by using the regexp template expression or calli ## Related links - [Ballerina regular expression grammar](https://ballerina.io/spec/lang/master/#section_10.1) - [RegExp langlib functions](/learn/by-example/regexp-operations) -- [RegExp API Docs](https://lib.ballerina.io/ballerina/lang.regexp) -- [string API Docs](https://lib.ballerina.io/ballerina/lang.string) +- [`regexp` langlib API Docs](https://lib.ballerina.io/ballerina/lang.regexp) +- [`string` langlib API Docs](https://lib.ballerina.io/ballerina/lang.string) +- [The `ensureType` function](/learn/by-example/ensureType-function/) diff --git a/examples/regexp-type/regexp_type.out b/examples/regexp-type/regexp_type.out index d5b8c9f0e8..cba26a1ac4 100644 --- a/examples/regexp-type/regexp_type.out +++ b/examples/regexp-type/regexp_type.out @@ -1,4 +1,6 @@ $ bal run regexp_type.bal +Pattern `[a-zA-Z0-9]` matches `a`: true +Pattern `[a-zA-Z0-9]` matches `a1`: false Pattern `HELLO*` matches `HELLO`: true Pattern `a+` matches `aaa`: true Pattern `abc|${"xyz"}` matches `xyz`: true