I am trying to match regardless of case.
regexp:RegExp pattern = re `(ax|test)is$`;
Should match both Axis or axis.
On
You can use following code snippet to achieve your task in Ballerina
import ballerina/io;
import ballerina/lang.regexp;
string str1 = "Axis";
string str2 = "axis";
string str3 = "aXIs";
string str4 = "aXIss";
public function main() {
regexp:RegExp pattern = re `(?i:(ax|test)is$)`;
io:println(pattern.isFullMatch(str1)); // true
io:println(pattern.isFullMatch(str2)); // true
io:println(pattern.isFullMatch(str3)); // true
io:println(pattern.isFullMatch(str4)); // false
}
Ballerina uses non-capturing groups to control the behavior of regular expression patterns.
So the above regex pattern contains a non-capturing group pattern and it have i flag inside the parentheses.
This i flag makes the pattern case-insensitive.
(?i:<pattern>) means that, the pattern will match without considering the case of the pattern.
You can find more details about non-capturing groups and flags of Ballerina in here.
Use an inline modifier to your regex pattern:
The
(?i:...)at the beginning does the trick of turning on case-insensitive mode for the entire pattern. Your code would functions like:An example might be:
This code will output: