在进行 ballerina 正则表达式匹配时我们可以忽略大小写吗

Niv*_*ika 6 regex ballerina

无论大小写,我都试图匹配。

regexp:RegExp pattern = re `(ax|test)is$`;
Run Code Online (Sandbox Code Playgroud)

应匹配 Axis 或 axis。

小智 4

对正则表达式模式使用内联修饰符:

regexp:regexp pattern = re `(?i:(ax|test)is$)`;
Run Code Online (Sandbox Code Playgroud)

开头(?i:...)的技巧是为整个模式打开不区分大小写的模式。你的代码的功能如下:

  1. (?i: : 设置不区分大小写的匹配。
  2. (ax|test):该组匹配“ax”或“test”。
  3. is$):确保匹配以字母“is”结尾。

一个例子可能是:

import ballerina/io;
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.isFullMatch(word1)) {
        io:println("Match: " + word1);
    }

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

    if (pattern.isFullMatch(word3)) {
        io:println("Match: " + word3);
    }
}
Run Code Online (Sandbox Code Playgroud)

该代码将输出:

Match: Axis
Match: axis
Match: TestiS
Run Code Online (Sandbox Code Playgroud)