File: match.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (45 lines) | stat: -rw-r--r-- 1,476 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

Puppet::Parser::Functions.newfunction(
  :match,
  :arity => 2,
  :doc => <<~DOC
    Matches a regular expression against a string and returns an array containing the match
    and any matched capturing groups.

    The first argument is a string or array of strings. The second argument is either a
    regular expression, regular expression represented as a string, or Regex or Pattern
    data type that the function matches against the first argument.

    The returned array contains the entire match at index 0, and each captured group at
    subsequent index values. If the value or expression being matched is an array, the
    function returns an array with mapped match results.

    If the function doesn't find a match, it returns 'undef'.

    **Example**: Matching a regular expression in a string

    ~~~ ruby
    $matches = "abc123".match(/[a-z]+[1-9]+/)
    # $matches contains [abc123]
    ~~~

    **Example**: Matching a regular expressions with grouping captures in a string

    ~~~ ruby
    $matches = "abc123".match(/([a-z]+)([1-9]+)/)
    # $matches contains [abc123, abc, 123]
    ~~~

    **Example**: Matching a regular expression with grouping captures in an array of strings

    ~~~ ruby
    $matches = ["abc123","def456"].match(/([a-z]+)([1-9]+)/)
    # $matches contains [[abc123, abc, 123], [def456, def, 456]]
    ~~~

    - Since 4.0.0
  DOC
) do |_args|
  Puppet::Parser::Functions::Error.is4x('match')
end