How to stop ForEach loop after a certain line?


I uses a regular expression, “(([^)]+))” , to match bracket.

How do I stop the ForEach loop after a certain line ?


The end

How can I stop the loop after the string ‘The end’ ?

Thank you

The solution work for me, however I switch the if else condition

Hi! I think you can use a “Break” activity within an “If” activity, after the code fulfills a certain condition.

1 Like

Hi @Nisa,

I uses the Matches activity to match bracket in the Words Document.

After I get the result of the Matches activity, I put the result in a ForEach loop.

If the item contains ‘mark’, I will extract the number.


(2 marks)
(5 marks)
The end
(1 mark)

I have tried using the Break activity
Eg. If the item.contains(“The end”), it will break the loop

However it did not works as the word “The end” is not inside a bracket

Is there a way to stop the loop after the string ‘The end’ ?

Thank you

What do you mean it does not work because it is not in a bracket?

The condition item.contains(“The end”) is also not in a bracket right? It should still match the string. May I know where you placed the break activity? It should be at the start, before you use the regex.

Since you are filtering the items using the Bracket and keeping the filtered items inside the for each. "The End will not come into the For each Loop.
So First you pass all the line items into the for each, there you can give if condition "Item,Contains(“The end”), so it will break the loop. In else condition you can do filters based on the bracket.

Hi @Nisa and @hemanth_chinna,

Thanks for your suggestion :slight_smile:

I will try and get back to you all!

Thank you

Hi @hemanth_chinna,

I have tried your suggestion.

If the item contains “The end”, in the then condition it will break.
If it does not contains “The end”, in the else condition it will use Matches activity to match bracket in Words Document, and put the Matches result in ForEach loop and get the number.

This is the outcome
When the line does not contains “The end”, it did go to the then condition.
However, it will stay in the then condition until it get all numbers.
After getting all numbers, it will go through the next line in the if condition.


The start
Next line
(2 marks)
(4 marks)
(3 marks)
The end

The first line “The start” is not the word we want, so it go to the else condition.
In the else condition, it will get all numbers (2, 4 and 3).
The next line is “Next line”, and it will get all numbers again.

Sorry if my explanation is confusing :sweat_smile:
Why does it keep repeating getting all the numbers ?
Is there anywhere i did wrongly ?

Thank you

I’d suggest uploading a sample XAML to demo what you are trying and it may make it easier to understand.

Based on the previous responses and your initial post, I can see it in one of two ways


Split the content by line
Loop over line by line
    Check if line contains "The End"
    If yes, break/continue/or whatever else you want to do
    If No, Use your regex and do what you want, and continue

2. Alt

Alter your regex to have an OR (([^)]+)|The End|Next line|The start)
loop over your matches
    Add condition if regex subgroup = "The End", break/continue/or whatever else you want to do

Hi @codemonkee,

Thank you for your suggestions! :slight_smile:

I will try and get back to you again!

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