Skip to main content

Notifications

Community site session details

Community site session details

Session Id : Y0XkrjKKDefscwvAYtFRKC
Power Automate - Power Automate Desktop
Answered

regex() isn't finding 6 digit number invoice

Like (2) ShareShare
ReportReport
Posted on 8 Oct 2024 09:55:05 by 46
I have Power Automate Desktop v2.48.153 on Windows 10. I'm trying to extract data from a PDF which has many invoices. I need to save each individual invoice as it's own PDF using the invoice number as the file name of the output PDF files. 
 
Here are the relevant steps I do.
 
- Find the line with "Customer name" in it, and get the next line which has the invoice number. 
- Split the line with the invoice number by spaces into a list. 
- Loop through that list to find a 6 digit number which is the invoice number, using the regex() function. This is the part that is not working when I step through the steps and look at the variables. If the invoicenum is found then assign it to the variable InvoiceNum and exit the loop early. 
 
Here's how I search for that invoice number in the line of data which has it: 
 
- If regex(variables(%CurrentItem%), '^\d{6}$') equal to True then 
    - set variable InvoiceNum to CurrentItem and exit loop early. This never gets executed. I've also tried this as my regex by having 0 or more spaces before and after the CurrentItem: regex(variables(%CurrentItem%), '^ *\d{6} *$')
 
Could there be some hidden characters in there that I have to filter out? How would I do that? 
What could I be doing wrong here? 
 
It seems the regex() function isn't working. I'm pretty familiar with regex as well. 
 
Thank you.
Categories:
  • Suggested answer
    eetuRobo Profile Picture
    3,280 Super User 2025 Season 1 on 08 Oct 2024 at 14:22:41
    regex() isn't finding 6 digit number invoice
    Have if condition that checks if Match is empty. If it is then Next loop.


    Empty string syntax for PAD is: %''%


    By the way are you sure you need to loop the line that has the Invoice number? Maybe reconsider removing the split and for each loop and just use regex to the line after the Customer name.
    So after you have found the line that would have the Invoice number (one after “Customer name” in it) just use Parse text with regex to that line. Also do not have “First occurrence only” checked so it returns all the matches from that row as an array.



    Then if there are many Matches found then do the split and loop. If there is only one match foun (%Matchers.Count% = 1) Then take the first and only Matches value. If there is no match found then notify that the PDF had no invoice number.

    Like so:



    ​​​​​​​
  • crob Profile Picture
    46 on 08 Oct 2024 at 12:49:06
    regex() isn't finding 6 digit number invoice
    Here's the section of flow I'm still having problems with. 
     
     
    Sometimes InvoiceNum is blank because there is no match. So Convert Number To Text fails with an error "InvoiceNum is not a number." Since I'm so new to PA I'm not sure how to get past this. I'm just not familiar with all the details of PA yet. 
     
    Could someone give me some guidance? 
  • Suggested answer
    Deenuji_Loganathan_ Profile Picture
    6,105 Super User 2025 Season 1 on 08 Oct 2024 at 12:06:58
    regex() isn't finding 6 digit number invoice
     Hi,
     
    Please follow as @eetuRobo suggested, you would be able to compare the string length.
     
    Your flow screenshot:


    Why isn't your code working?
    1. It's because you read the match value and assigned it to invoicenum.
    2. Once you assign the invoice number to the invoicenum variable, it becomes a Number type variable.
    3. In Power Automate Desktop, the Number data type doesn't have a length property.
    4. So, you need to convert the invoicenum variable to a string data type using the "Convert number to text" action.
    5. After that, you will have the length property available for your variable.
     
     
     
     
    Thanks,
    Deenu
     
  • Suggested answer
    eetuRobo Profile Picture
    3,280 Super User 2025 Season 1 on 08 Oct 2024 at 11:46:44
    regex() isn't finding 6 digit number invoice
    You get length like this:
    %InvoiceNum.Length%

    So inside the % signs have the variable and .Length



  • crob Profile Picture
    46 on 08 Oct 2024 at 11:36:52
    regex() isn't finding 6 digit number invoice
    Thank you. I did not know the functions were in preview. I'm still new to Power Automate. 
     
    My Flow is finding the invoice number (on one test I did) but now I want to exit the loop if the LENGTH(%InvoiceNum%) = 6, and it's not exiting the loop. Even though the invoice number found might be "333444"  the exit loop action is not executing. I don't want to continue the loop if I find exactly 6 digits as there may be more digits which are not the invoice number. 
     
  • Suggested answer
    Deenuji_Loganathan_ Profile Picture
    6,105 Super User 2025 Season 1 on 08 Oct 2024 at 10:23:17
    regex() isn't finding 6 digit number invoice
    If you are utilizing Power Fx expressions, be aware that they are still in preview and may not function as expected on many occasions, and also have numerous limitations. Therefore, rather than using Power Fx, consider following the alternative approach outlined below(This is power fx disabled flow):
    Code(Copy and paste the below code into your power automate desktop flow):
    SET CurrentItem TO 453456
    **REGION Check if the Currentitem variable has six digits and show a message box if it does
    Text.ParseText.RegexParse Text: Currentitem TextToFind: $'''\\d{6}''' StartingPosition: 0 IgnoreCase: True OccurrencePositions=> OccurrencePositions Matches=> Matches
    IF Matches.Count > 0 THEN
        Display.ShowMessageDialog.ShowMessage Title: $'''Validation''' Message: $'''The Currentitem variable contains six digits.''' Icon: Display.Icon.Information Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed
    END
    **ENDREGION
    
     
     
    Thanks,
    Deenu
  • Suggested answer
    eetuRobo Profile Picture
    3,280 Super User 2025 Season 1 on 08 Oct 2024 at 10:17:23
    regex() isn't finding 6 digit number invoice
    Power Fx is still in preview and does not have all the Power Fx expressions. I would recommend not using Power Fx enabled flows if you don't need to. 

    But either way if you do use regular flow or power fx enabled flow use Parse text -action where you toggle on "Regular Expression".
    Like so:

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Power Automate Desktop

#1
eetuRobo Profile Picture

eetuRobo 25 Super User 2025 Season 1

#2
Nived_Nambiar Profile Picture

Nived_Nambiar 14 Super User 2025 Season 1

#3
KO-05050229-0 Profile Picture

KO-05050229-0 6

Overall leaderboard
Loading started