'How to know when Regex requires 2 arguments or 3 (with text replacement)

To extract data using regex in excel, 2 functions have been suggested in previous questions from the following posts:

https://gist.github.com/Hugoberry/4948d96b45d6799c47b4b9fa1b08eadf https://www.mathscinotes.com/2020/02/power-query-regular-expression-hack/

fnRegexExtr2

The first function requires 2 arguments (Text from Column, Regex):

let   fx=(text,regex)=>
    Web.Page(
        "<script>
            var x='"&text&"';
            var y=new RegExp('"&regex&"','g');
            
            var b=x.match(y);
            document.write(b);
        </script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}

in
fx

fnRegexExtr3

The second function requires 3: (X(Text From Column), Y(Regex) Z(Text to Replace))

(x,y,z)=>
let 
   Source = Web.Page(
                     "<script>
                     var x="&"'"&x&"'"&";
                     var z="&"'"&z&"'"&";
                     var y=new RegExp('"&y&"','g');
                     
                     var b=x.replace(y,z);document.write(b);</script>")
                     [Data]{0}[Children]{0}[Children]{1}[Text]{0}
in 
   Source

Building on an answer to my previous question the second function works well to separate out Pressure values from a string of text as shown:

Arguments:

  • x - Input Text from Column
  • y - (\\d+(?:-\\d+)?)\\D*mmHg|.
  • z - $1

M Code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Changed Type", "fnRegexExtr3", each fnRegexExtr3([Column1], "(\\d+(?:-\\d+)?)\\D*mmHg|.", "$1 "))
in
    #"Invoked Custom Function"

enter image description here

When I apply the same Regex to the first function that only takes two parameters, the letters are completely separated out:

M Code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Changed Type", "fnRegexExtr2", each fnRegexExtr2([Column1], "(\\d+(?:-\\d+)?)\\D*mmHg|."))
in
    #"Invoked Custom Function"

enter image description here

Update: Answer? The issue appears to lie with |. at the end of the second argument. fnRegexExtr2 works with (\\d+(?:-\\d+)?)\\D*mmHg alone.

enter image description here

It appears that fnRegexExtr3 is required to only extract the values which now make sense.

I hope this will be useful to anyone else. (Feel free to correct me if I'm wrong or add to this discussion).

Data if you want to repeat any of the above tests:

On a positive note, the intraocular pressure had dropped to 10mmHg in this eye showing that the topical Azopt is effective in reducing the intraocular pressure.

At the moment the IOP is normal at 16mmHg in both eyes.Anincidental finding was an optic nerve coloboma in the left eye, a congenital abnormality, for which notreatment is indicated.

The eye was uncomfortable as the intraocular pressurewas 60mmHg (normal range for the dog is 15-25mmHg).

Examination of the right eye confirmed that it was comfortable and visual with a normal intraocular pressure(20mmHg).


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source