Difference between single pipe and double pipe in Raku Regex (| Vs ||)
Asked Answered
G

1

9

There are two types of alternation in Raku's regex: the | and ||. What is the difference ?

say 'foobar' ~~ / foo || foobar /  # 「foo」
say 'foobar' ~~ / foo | foobar /   # 「foobar」
Giamo answered 19/3, 2020 at 2:0 Comment(0)
G
12
  • The || is the old alternation behaviour: try alternation from the first declared to the last

  • The | try alternation from the longest to the shortest declarative atom. It is called the Longest Token Matching Spec strategy.

say 'foobar' ~~ / foo || foobar /  # 「foo」 is the first declared
say 'foobar' ~~ / foo | foobar /   # 「foobar」 is the longest token

More detailed answer in this post

Giamo answered 19/3, 2020 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.