Skip to content

Commit

Permalink
Merge pull request #5767 from MaryamZi/update-regexp-type-bbe
Browse files Browse the repository at this point in the history
Update the RegExp type BBE
  • Loading branch information
MaryamZi authored Oct 24, 2024
2 parents e31c81a + 6b88fc9 commit ea62564
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
27 changes: 18 additions & 9 deletions examples/regexp-type/regexp_type.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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));
Expand All @@ -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εЛ`: ", (<regexp:Span>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));
Expand Down
7 changes: 4 additions & 3 deletions examples/regexp-type/regexp_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :::

Expand All @@ -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/)
2 changes: 2 additions & 0 deletions examples/regexp-type/regexp_type.out
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ea62564

Please sign in to comment.