Uploaded image for project: 'Puppet'
  1. Puppet
  2. PUP-9554

Add ability to create regexp with all special chars escaped

    Details

    • Template:
    • Sub-team:
    • Team:
      Server
    • Release Notes:
      New Feature
    • Release Notes Summary:
      Hide
      It is now possible to create a Regexp that escapes all regexp special characters by adding a boolean `true` as a second argument in a call to `new`. For example `Regexp(".[/", true)` or `Regexp.new(".[/", true)`. A second value of `false` is the default.
      Show
      It is now possible to create a Regexp that escapes all regexp special characters by adding a boolean `true` as a second argument in a call to `new`. For example `Regexp(".[/", true)` or `Regexp.new(".[/", true)`. A second value of `false` is the default.
    • QA Risk Assessment:
      Needs Assessment

      Description

      Original


      Currently it is impossible to just pass a String to split and split by that string, for example:

      split($var,'.')

      won't work as the string is treated as regexp and is basically equivalent of split($var,/./)

      While for dot it is trivial to escape (altho still surprising considering that's not how split function usually works in other languages), for more complex strings it is annoying at best (as there is AFAIK no "regex escaping" function in puppet.

      My proposition is to either

      • Make strings do not act as regexp. Types are there for a reason, if someone wants to split by regexp they should pass a regexp as argument
      • Make strings be interpolated to regexp only when they start and end with / and have something inbetween (so split($var,"/./")) gets interpreted as regexp but  split($var,"//") does not)

      Update


      Best course of action here is to add the ability to create a Regexp where all special characters are escaped. This is easily done by adding a flag to Regexp.new. Then, if the resulting string is needed (to be concatenated with other strings to form the final regexp), that can be achieved by using String.new(Regexp("...", true)) - i.e. by converting the created escaped regexp to a String.

        Attachments

          Activity

            jsd-sla-details-panel

              People

              • Assignee:
                Unassigned
                Reporter:
                xani Mariusz Gronczewski
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Zendesk Support