Skip to content

Zero length regular expression match misbehaviour #2565

@weeble

Description

@weeble

Description

Some regular expressions behave weirdly when matches are zero length - they miss matches or match too often.

Reproduction

Case 1

Input:

jq --null-input --compact-output '"xx"|match("";"g")'

Output:

{"offset":0,"length":0,"string":"","captures":[]}
{"offset":1,"length":0,"string":"","captures":[]}

Expected:

{"offset":0,"length":0,"string":"","captures":[]}
{"offset":1,"length":0,"string":"","captures":[]}
{"offset":2,"length":0,"string":"","captures":[]}

There should be a match at the end of the string too.

Case 2

Input:

$ jq --null-input --compact-output '"xx"|match("$";"g")'

Output:

{"offset":2,"length":0,"string":"","captures":[]}
{"offset":2,"length":0,"string":"","captures":[]}

Expected:

{"offset":2,"length":0,"string":"","captures":[]}

The same match shouldn't show up twice. (Extra matches seem to be returned in number equal to the length of the string, for some reason. But they're all the same match at the same offset.)

Environment

  • OS and Version: Ubuntu
  • jq version: master branch, commit cff5336

Additional context

I stumbled on this trying to fix #2148 and found that my attempted solution didn't behave like I expected it to. I tried to isolate the problem and ended up with these.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions