: ex and: ov adverbs with Perl 6 with names

I don’t quite understand why the results here are different. Does it use :ov only for <left> , so finding the longest match would it do nothing?

 my regex left { a | ab } my regex right { bc | c } "abc" ~~ m:ex/<left><right> {put $<left>, '|', $<right>}/; # 'ab|c' and 'a|bc' say '---'; "abc" ~~ m:ov/<left><right> {put $<left>, '|', $<right>}/; # only 'ab|c' 
+5
source share
2 answers

Adverb Types

It is important to understand that there are two different types of repressive dialects:

  • Those that fine tune how your regular expression code is compiled ( :sigspace / :s :ignorecase / :i , ...). They can also be written inside the regular expression and apply only to the rest of their lexical area within the regular expression.
  • Those that control how regular expression matching is found and returned (for example :exhaustive / :ex :exhaustive :overlap / :ov :exhaustive :global / :g ). They are applied to the given regular expression matching operation in general and should be written outside the regular expression as an adverb of the operator m// or .match .

Matching Adverbs

Here's what the second type of adverbs do:

  • m:ex/.../ finds every possible match in all possible starting positions.
  • m:ov/.../ finds the first possible match at each possible starting position.
  • m:g/.../ finds the first possible match in each possible starting position that comes after the end of the previous match (that is, it does not overlap).
  • m/.../ finds a possible match first at the starting position of the first .

(In each case, the regex engine moves as soon as it discovers that it should have been found at any given position, so you don’t see any additional output, even if you put print statements inside regexes.)

Your example

In your case, there are only two possible matches: ab|c and a|bc .
Both start at the same position in the input line, namely at position 0.
Thus, only m:ex/.../ will find both of them: ndash; all other options will find only one of them, and then go over.

+6
source

:ex will find all possible combinations of matching matches.

:ov acts like :ex , except that it limits the search algorithm by restricting it to find only one match for a given starting position, forcing it to produce one match for a given length. :ex allowed to start from the very beginning of the line to find a new unique match, and therefore it can find several matches of length 3; :ov will find only one match of length 3.

Documentation:
https://docs.perl6.org/language/regexes

Exhaustive :

To find all possible matches of a regular expression - including overlapping ones - and several that begin in the same position, use: an exhaustive (short: ex) adverb

Overlap :

To get multiple matches, including matching matches, but only one (the longest) from each starting position, specify the adverb: overlap (short: ov):

+6
source

Source: https://habr.com/ru/post/1272854/


All Articles