Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

pattern without ^ and $ matches nothing unless Surrounded #20

Description

@starius
  1. There are two functions called Matches.
  • from pire/run.h:
bool Matches(const Scanner& scanner, const char* begin, const char* end)
{
        return Runner(scanner).Run(begin, end);
}
  • from README:
bool Matches(const Pire::NonrelocScanner& scanner, const char* ptr, size_t len)
{
        return Pire::Runner(scanner)
                .Begin()        // '^'
                .Run(ptr, len)  // the text 
                .End();         // '$'
                // implicitly cast to bool
}

Which one is correct?

If Begin() and End() are to be called, then patterns without ^ and $ match nothing:

Graph for pattern 'abc'

When Begin() is called, it feeds scanner with special begin char, moving it to dead state 1.

Compare this graph with the graph produced for same pattern surrounded and optimised:

Graph for pattern 'abc' surrounded and optimised

Does this mean that all patterns must begin with ^ and end with $? Are Begin() and End() calls required? It should be clarified and documented.

2 . pigrep

Program pigrep behaves as latter Matches, calling Begin() and End(). It also surrounds its patterns. I have removed surrounding (btw it would be useful option, grep has it as -x, --line-regexp) and get the following results:

$ echo -n 'abc' | pigrep 'abc'
$ echo -n 'abc' | pigrep '^abc'
$ echo -n 'abc' | pigrep 'abc$'
$ echo -n 'abc' | pigrep '^abc$'
abc

Summary of problems here:

  • pattern without ^ and $ matches nothing unless Surrounded
  • two different functions called Matches
  • provide pigrep option equivalent to grep --line-regexp

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions