Regex to remove entire lines between 2 specifc identifiers

Hi Team,

In an input string we can have multiple occurrences of some specific keywords.
Is there a way we can remove all the lines including and in-between those keywords?

For below example, we have to look for the keyword '#end and traverse upto the line it first finds ); and delete all the lines [including lines which contains '#end and );]

Input ::

PD12"You have completed the survey. Thank you for your time."
info;

);

'#end

PR3"You have completed the survey. Thank you for your time."
info;

);
'#end

SD7"You have completed the survey. Thank you for your time."
info;

);

'#end

Output ::

PD12"You have completed the survey. Thank you for your time."
info;

PR3"You have completed the survey. Thank you for your time."
info;

SD7"You have completed the survey. Thank you for your time."
info;

You can try this pattern with the Assign activity using Regex.Replace:

newText = System.Text.RegularExpressions.Regex.Replace(inputText, "\);[\s\S]*?'#end", "")

1 Like

@ptrobot … Thanks for your quick response.
With the regex you shared, it is working fine when we have the exact input.
But in-case there is another ); in the previous line, it is selecting that line also (Please refer to the attached snap).
Will you be able to modify this regex which only select up-to 1st occurrence of ); when it’s start traversing upwards from '#end ?

Then the pattern should be like this:

\);(?![\s\S]\);)[\s\S]*?'#end

1 Like

I have randomly added some extra text in-between the lines having ); - it’s failing here…blockend_error

Sorry, forgot a *

\);(?![\s\S]*\);)[\s\S]*?'#end

1 Like

Here, we have total 3 occurrences of '#end.
Here what we need is -
Whenever it will find the keyword '#end, it will start traversing upwards till it finds ); [need to consider only upto 1st occurrence of );] and delete all the lines [including lines which contains '#end and );].
With the updated regex, it is working fine for 3rd occurrence, but for 1st & 2nd one it is not selecting anything…

Please let me know if I am still unable to clarify the requirements :slight_smile:

Really appreciate the help you are providing here @ptrobot

1 Like

@DewanjeeS – Yup…I was about to check that with @ptrobot on the same…Sorry if I am missing anything here…

2 Likes

Thanks for the explanation but Regex can’t find '#end and go up. What it does is to find ); and then go down until it finds '#end. If we don’t want xx in between we need to use (?!xx) to tell it not to match xx.

I hope it’s right this time:

\);((?!\);)[\s\S])*?'#end

3 Likes

Perfect!! exactly what I was looking for.
Thanks a ton @ptrobot

1 Like

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