Get a specific item of the regex.matches result

I’d like to share my workflow on how I have gotten a specific number from a website which coudn’t be gathered with any string manipulation but with using regex.matches.

The problem was that the string was either “1–16 von 117 Ergebnissen” or " 1 Ergebnis" and i needed only the last number for the further workflow.

E.g. 117 or 1.

I have scraped the text from a website with the get text activity. Then I have used the regex.matches activity to extract only digits with a length of at least 1.

RegexPattern -> “(\d+)”
Result = Reg_FoundCounterMatches

After doing so, while in debugging mode, I can see that 3 items have been captured and are now stored in the Result.

First the length of the Reg_FoundCounterMatches is needed in a variable, to use it as an Index for finding the last item of the Reg_FoundCounterMatches.)

FoundCounterMatches_Length = Reg_FoundCounterMatches.count (as int32)

Keeping in mind that the last item of the regex collection has to be found and that the regex.matches is a zero-based collection but the counted length is not, the FoundCounterMatches_Length has to be decreased by 1.

LastItemCounter=FoundCounterMatches_Length-1 (as Int32)

The Solution is:

DesiredItem=Convert.ToInt32(Reg_FoundCounterMatches(LastItemCounter).toString)

Conversion to String in brackets has to be done so that the conversion to Int32 will be successful.

I hope it will help at occasion

Happy Automation!

Hi @Tim_Gentemann - Based on the two values provided I have come up with the below regex

You have to print the group2 values like ==> YourRegoutputVar(0).groups(2).tostring to get the desired output…

1 Like

Thanks for your reply and the complement.

As I only get one of the solution “1–16 von 117 Ergebnissen” or " 1 Ergebnis" my workflow throws the correct answer.
Regex

But if there were two your solution would be very helfpful! Thanks again :slight_smile:

@Tim_Gentemann - Reason I put both in the Regex tool is to show that both the cases are working… issue solved or not? if solved, Please mark my post as solution, as it helps others…

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.