Green-Software-Foundation/if-plugins

`regex` plug-in only returns one matched value after regular expression matches.

pangteckchun opened this issue · 2 comments

Description of the Error

While using regex plug-in to extract part of instance type parameter value, only a single matched value is returned from matches.

Expected Behaviour

Given this input string "Standard_DS2_v1" and the regular expression "/(?<=)[^_]+?(?=|$)/g", I wanted it to return "DS2 v1" of the 2 matched elements with a space in between.

Actual Behaviour

The return result is only "DS2" , first matched element but without the second matched element "v1" in the total result.

Steps to Reproduce

Run regex plug-in using IF manifest (output section shown below):

name: regex-demo
description: null
tags: null
initialize:
  plugins:
    regex:
      path: '@grnsft/if-plugins'
      method: Regex
      global-config:
        parameter: cloud/instance-type
        match: /(?<=_)[^_]+?(?=_|$)/g
        output: cloud/instance-type
  outputs:
    - yaml
if-version: v0.3.1
tree:
  children:
    child:
      pipeline:
        - regex
      config:
        regex: null
      inputs:
        - timestamp: 2023-08-06T00:00
          duration: 3600
          cloud/instance-type: Standard_DS1_v2
      outputs:
        - timestamp: 2023-08-06T00:00
          duration: 3600
          cloud/instance-type: DS1

Link to online environment

Used https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#try_it to try out the same string and regex inputs, since the plug-in typescript leverages the standard String prototype match() method.

const paragraph = 'Standard_DS1_v2';
const regex = /(?<=_)[^_]+?(?=_|$)/g;
const found = paragraph.match(regex);

console.log(found);
console.log(found[0]);
// Produces output: Array ["DS1", "v2"]
// And "DS1"

Looking at https://github.com/Green-Software-Foundation/if-plugins/blob/main/src/lib/regex/index.ts, it returns only the 1st array object in extractMatching block, which is incorrect.

Additional qns: if the entire array is returned, how can we concatenate the elements together or can regex do that for us, with space between the elements?

Manifest File That Generated the Error

name: regex-demo
description: null
tags: null
initialize:
  plugins:
    regex:
      path: '@grnsft/if-plugins'
      method: Regex
      global-config:
        parameter: cloud/instance-type
        match: /(?<=_)[^_]+?(?=_|$)/g
        output: cloud/instance-type
  outputs:
    - yaml
if-version: v0.3.1
tree:
  children:
    child:
      pipeline:
        - regex
      config:
        regex: null
      inputs:
        - timestamp: 2023-08-06T00:00
          duration: 3600
          cloud/instance-type: Standard_DS1_v2

Links to Any Additional Code

NA

Runtime Info

Running on local laptop: Windows OS 10
IF version:

+-- @grnsft/if-plugins@v0.3.0
+-- @grnsft/if-unofficial-plugins@v0.3.1
`-- @grnsft/if@v0.3.1

Hi @pangteckchun - sorry this isn't working for you. It's not really a bug as the plugin is only intended to give the first match. Appreciate you need multiple matches - so what we'll do is close this bug report and open a feature request instead and build out the multiple match functionality in the coming sprints.

Tagging myself @jmcook1186 as a reminder to make the dev ticket!

Thank you James for following up! Appreciate this.
Noted the original feature is to only return the 1st match. For now I will workaround using my own CLI program integrating it to the my pipeline using shell plug-in.

Looking forward to its future evolution.
Cheers!