web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / REGEX Capture Groups v...
Power Automate
Answered

REGEX Capture Groups via Script - Javascript or Python

(0) ShareShare
ReportReport
Posted on by 51

Hi Everyone,

 

I need to run a REGEX through a large text file (very badly formatted CSV with oodles of issues in parsing) and return the results to PAD. The only way I can see to achieve this is via either JS or Python script. I am currently working with a JS implementation using the following:

 

Set Variable called %REGEX% = ^(?<SKU>[\\d\\w]+),\\d*,\"?(?<Description>.+)\"?,\\$(?<Price>[\\d\\.]+)

Read Text from File into %FileContents%

Run Javascript&colon;

var csv = %FileContents%;
var reg = %RegEx%;
var out = csv.matchAll(reg);
WScript.Echo(out);

 

This does not work 😞

 

I receive the error (truncated) --> Microsoft JScript compilation error: Expected ';'

 

Can anyone point me in the right direction to solving this? I'm guessing that it is something syntactic with the REGEX itself but I am not sure how to debug this particular scenario.

 

Any help greatly appreciated.

 

Cheers

 

MisterH

 

I have the same question (0)
  • MisterH Profile Picture
    51 on at

    PS - The REGEX pattern works perfectly for it's intended task (created and tested in REGEX Buddy)

  • VJR Profile Picture
    7,635 on at

    In javascript every line should end with a semicolon ;

    So correct that and if still facing issues then post the original script here to check for errors.

  • VJR Profile Picture
    7,635 on at

    I think after re-reading now I understood your above code better.

    The first three lines are your PAD actions and the rest is your Javascript code? Is it?

    Also check out "Parse Text" which has an option to turn on regex and generate the output.

  • MisterH Profile Picture
    51 on at

    Hi VJR,

     

    Yes, first three lines are the steps in PAD.

    1/ Set a variable to hold the REGEX pattern

    2/ Read the text 'raw' from the file

    3/ Run the JavaScript step below

     

    The remaining lines are the JS itself, with %FileContents% and %RegEx% as the PAD variables holding their respective data, and being passed from PAD to the JS step.

    var csv = %FileContents%;
    var reg = %RegEx%;
    var out = csv.matchAll(reg);
    WScript.Echo(out);

     

     Sorry for the lack of clarity previously. I hope this explains things a little better.

     

    MisterH

  • VJR Profile Picture
    7,635 on at

    No issues, have you checked out the Parse Text action I have mentioned above. It has an in-built option where you can pass the regex and get the output.

  • MisterH Profile Picture
    51 on at

    PS (again) - the Parse Text step is going to be very inefficient for processing the data. Each one of these files has more than 16k lines, and there are going to be a continuous stream of them all poorly formatted due to the process that produces them - beyond my control I'm afraid. The number of data points I need to extract per line is 3 (as you can see from the capture groups in the REGEX), however handling this in a single step would be a vast improvement and in theory should be possible with either JS or Python from what I can tell. I'm just not sure how to correctly pass in the data for it to work.

  • VJR Profile Picture
    7,635 on at

    I read this message in your original post but could not co-relate it with the code written in JS.

    Didn't co-relate the second time too. Its not about your explanation but about me :).

     

    I think below is the same equivalent of what you are trying to achieve via the javascript code.

    When you disable the "First occurrence only", the matches variable will return multiple matches.

    Post back if it much more complicated.

     

    VJR_0-1657003634781.png

     

  • MisterH Profile Picture
    51 on at

    Here is a sample data set with some of the 'cleaner' data that is available in one of the files:

     

    ACEFOBKIT,,"CS ACE WIEGAND 1 DOOR KIT, INC CS4890 CONTROLLER, HID READER & 10 X SEOS FOBS",$123.00,1 Door Kits,CST,
    ACEFOBKITSTART,458922514,"CS ACE WIEGAND 1 DOOR KIT, INC CS4890 CONTROLLER, PROG KEYPAD HID READER & 10 X SEOS FOBS",$123.00,1 Door Kits,CST,
    CS4836,,CS iKEY 1 DOOR KIT 4836 with 10 iKEYS,$123.00,1 Door Kits,CST,
    CS4828,,CS iKEY 2 DOOR KIT 4828 with 10 iKEYS,$123.00,2 & 4 Door Kits,CST,

     

    and yes, there is a comma(,) at the end of every line.

  • VJR Profile Picture
    7,635 on at

    As a side note to my above screenshot on Parse Text, to access PAD variables in a script enclose quotes around it

     

    var csv = "%FileContents%";

  • MisterH Profile Picture
    51 on at

    Parse Text can do multi-match, but it doesn't seem to handle multiple capture groups and simply returns the string (entire length) between where the match starts and finishes. I need to specifically extract the capture groups, which would mean running Parse Text 3 times per line of text. This means on a 16K line file there are 48k steps inside of a for each loop, giving a total of 80k steps in total. Horrifically inefficient.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 632

#2
Haque Profile Picture

Haque 458

#3
Valantis Profile Picture

Valantis 357

Last 30 days Overall leaderboard