Hacker News new | past | comments | ask | show | jobs | submit login

It could be clearer if you used Raku regexes

    my $pattern = /:sigspace
      ^
      score  $<points>     = (\d+)
      for    $<nights>     = (\d+)  nights?
      at     $<hotel-name> = (.*)
      $
    /

    if 'score 400 for 2 nights at Minas Tirith Airport' ~~ $pattern {
      say "the score was $<points>";
      say "it occurred over $<nights>";
      say "at $<hotel-name>";
    }
Or if you really wanted to be ambitious, a grammar

    grammar Pattern {
      token points {\d+}
      rule  nights {<(\d+)> nights?}
      token hotel-name {.*}
      rule TOP {
        score <points> for <nights> at <hotel-name>
      }
    }

    if Pattern.parse('score 400 for 2 nights at Minas Tirith Airport') {
      say "the score was $<points>";
      say "it occurred over $<nights>";
      say "at $<hotel-name>";
    }
Note that only the `s` is optional `?` in `points?`. To make the whole thing optional would require grouping it in some manner `[points]?` or even `"points"?`



Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: