Search code examples
regexballerina

Ballerina - Where is Span defined?


I am trying to assign the return value of regexp:findAll to a variable, and I cannot seem to figure it out. The documentation at that previous link says that the return type is Span[] but the compiler doesn't seem to know anything about that type. Here is a code fragment:

    string:RegExp word_char = re `\w`;
    Span[] spans = word_char.findAll("test string");

And I get these errors:

| error: unknown type 'Span'
|   Span[] spans = word_char.findAll("test string");
|   ^--^
| error: incompatible types: expected 'other[]', found 'ballerina/lang.regexp:0.0.0:Span[]'
|   Span[] spans = word_char.findAll("test string");
|                  ^------------------------------^
| Compilation aborted due to errors.

Solution

  • Span is defined in the same module as the findAll function (lang.regexp). So it requires importing the ballerina/lang.regexp module and using a qualified identifier (with the regexp prefix).

    import ballerina/lang.regexp;
    
    public function main() {
        string:RegExp word_char = re `\w`;
        regexp:Span[] findAll = word_char.findAll("test string");
    }