Search code examples
regexjuliapcre

LoadError: PCRE compilation error: lookbehind assertion is not fixed length


When I try to write a parser in julia that uses a lookbehind pattern, it throws an PCRE compilation error.

function parser(str::String)
    a = match(r"^[a-zA-Z]*_[0-9]", str)
    b = match(r"(?<=[a-zA-Z]*_[0-9]_)[a-zA-Z]", str)
    a.match, b.match
end

parser("Block_1_Fertilized_station_C_position_23 KA1F.C.23")
# LoadError: PCRE compilation error: lookbehind assertion is not fixed length at offset 0

Can somebody explain what I'm doing wrong?


Solution

  • Julia uses Perl Compatible Regular Expressions (pcre), and as stated in the pcre documentation:

    Each top-level branch of a lookbehind must be of a fixed length.

    Meaning you can't use operators like * or + in a lookbehind pattern.

    So you'd have to figure out a pattern that doesn't use them. In your case the following might work:

    function parser(str::String)
        a = match(r"^[a-zA-Z]*_[0-9]", str)
        b = match(r"(?<=_[0-9]_)[a-zA-Z]*", str)
        a.match, b.match
    end
    
    parser("Block_1_Fertilized_station_C_position_23 KA1F.C.23")
    # ("Block_1", "Fertilized")