Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can we ignore case when doing ballerina regexp matches

I am trying to match regardless of case.

regexp:RegExp pattern = re `(ax|test)is$`;

Should match both Axis or axis.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use an inline modifier to your regex pattern:

regexp:regexp pattern = re `(?i)(ax|test)is$`;

The (?i) at the beginning does the trick of turning on case-insensitive mode for the entire pattern. Your code would functions like:

  1. (?i): Sets case-insensitive matching.
  2. (ax|test): This group matches either "ax" or "test".
  3. is$: Ensures the match ends with the letters "is".

An example mnight be:

import ballerina/regexp;

public function main() {
    regexp:regexp pattern = re `(?i)(ax|test)is$`;

    string word1 = "Axis";
    string word2 = "axis";
    string word3 = "TestiS";

    if (pattern.isMatch(word1)) {
        io:println("Match: " + word1);
    }

    if (pattern.isMatch(word2)) {
        io:println("Match: " + word2);
    }

    if (pattern.isMatch(word3)) {
        io:println("Match: " + word3);
    }
}

This code will output:

Match: Axis
Match: axis
Match: TestiS
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading